Just a question for all of you gurus out there. I am currently running insert and update SQL statements similar to the following:
rather than go through the tables twice (in some cases I have to perform several joins to update and insert which is making my tempdb grow quickly) in order to insert and update, is there a way I can search through the table once and perform either an insert or an update? I know in the Oracle world you can do a merge statement, is there an equivellant in the SQL world? I am essentially moving data from working tables to production tables but have keys and constraints to worry about when updating and inserting.
Thanks in advance
Code:
PRINT 'UPDATING Table1
UPDATE Table1
SET val1 = X.val1,
val2 = X.val2
FROM XML_Table1 X
WHERE Table1.VIN=X.VIN
PRINT 'INSERTING INTO Table1
INSERT Table1 (val1,
val2)
SELECT DISTINCT val1,
val2
FROM XML_Table1 X
WHERE NOT EXISTS
(SELECT seq_num FROM Table1 Y
WHERE Y.VIN=X.VIN)
rather than go through the tables twice (in some cases I have to perform several joins to update and insert which is making my tempdb grow quickly) in order to insert and update, is there a way I can search through the table once and perform either an insert or an update? I know in the Oracle world you can do a merge statement, is there an equivellant in the SQL world? I am essentially moving data from working tables to production tables but have keys and constraints to worry about when updating and inserting.
Thanks in advance