after running insert or update query, need the last inserted record and compare in vb.net sql server - vb.net

i have 2 queries in vb.net with an if clause -
if x=0 then
insert into table1
else
update table1
both queries have 5 fields. now what i want to do is after this insert or update takes place, i need to look at this inserted/updated record and compare it with another table (table2). Especially for update, i have 5 fields in both tables. if any of the 5 fields dont match with table2, then i insert a new record in table 2 which is the updated record in table 1.
how can i do this?

You could use a trigger and keep this all on the database.

Related

Insert from 2 tables into 1 table

I would like to insert columns from 2 different tables into one table.
The following is not working on the second Insert. Is this possible to do this way? the receiving table is currently empty. the idea is to have one row in the third table per the 2 inserts.
INSERT INTO CA1665AFTT.EMODESADV3
(E3ecsn, e3mena, e3hand)
SELECT e1ecsn, e1mena, e1hand
FROM CA1665AFTT.EMODESADV1
INSERT INTO CA1665AFTT.EMODESADV3
(E3CPRD, E3CQTY)
SELECT prdc, oqty
FROM Monica.emod
Yes it's possible using a join between the two tables

Incrementing 2 fields after inserting values based on another table?

This is an extension of Insert values into table B based on column from table A?
From the above question, let's say in both the User_Permissions and Users table there's also 2 more columns recorded for audit purposes: a version column and a transaction_version column. When inserting the new row (which is based on a row from the Users table) into the User_Permissions table I need to take the value of the 2 columns in the Users table, increment it by 1 and then insert it into the User_Permissions table.
Is there an easy SQL query to do this? I suspect it'd have to do with another inline select but am unsure of the syntax.
You could use Triggers after insert to perform the needed updates

How to insert data from two different tables using trigger

I have two tables with data. Now I have to insert selected fields of these two tables into a third table.
Table 1 will return me 1 row only.
Table 2 will have 5 records (say).
Now Table 3 should have 5 records with data from table 1 and table 2.
Is there a way of looping sql server trigger. Please answer my query as soon as possible.
Thanks and Regards.
You need trigger in both tables?
It's not possible to loop trigger.. a trigger is an event on a table. You can call a stored procedure and include the result of the trigger with a temporary view

INSERT based on another table's row

I need to INSERT a row in table_A depending on the information in a row in table_B.
Is it possible to do this in an isolated way where the SELECT retrieval of the row from table B is locked until either the new row is INSERTed into table_A or the INSERT is skipped due to the information in table_B's row?
It's really not clear what you are trying to say , i think your problem is solved by using a trigger .
check this site for know more about trigger
http://www.codeproject.com/Articles/25600/Triggers-SQL-Server
You can do this:
INSERT INTO A (columns) select columns from table B where condition;
Columns retrieved from the query must match the queries defined in the table A.
PostgreSQL supports MVCC, custom locking can be done but it is not recomended.

I've got to update a column in one SQL table with a counter stored in another table, and update that table too

I'm using SQL server 2005 (for testing) & 2007 (for production).
I have to add a unique record ID to all the records in my table, in an existing column, using a "last record ID" column from another table. So, I'm going to do some sort of UPDATE of my table, but I have to get the "last record ID" from the other table, increment it, update THAT table and then update my record.
Can anyone give me an example of how to do this? Other users may be incrementing the counter also.
if you are INSERTING into one table and then UPDATE(ing) the next table you can use ##IDENTITY to give you the auto increment ID from the first table.
E.g.
INSERT INTO table1 (description) VALUES ('blah');
UPDATE table2 SET (tabel1ID = ##IDENTITY) WHERE condition
##IDENTITY will give you the ID of the last inserted row.