Let's say I have a table and I want to insert a row. The new row's key may already match an existing row's key in the table, in which case I want to update the existing row. Or, it may not exist in the table, in which case the new row should be inserted.
What is the most efficient way to perform such an operation? I was thinking of first doing a SELECT
(perhaps with EXISTS
) to see if a particular key is present, followed by an UPDATE
if present and an INSERT
if not. You would probably need to keep an UPDLOCK
and a HOLDLOCK
for this combination of statements in order to avoid race conditions, as well. This seems overly complicated and inefficient.
I was wondering if there was a more efficient way to do this in SQL Server 2008R2.
Insert would be faster because in case of update you need to first search for the record that you are going to update and then perform the update.
2 Answers. Show activity on this post. mysql_query("INSERT INTO `test` (`name`) VALUES ('Mark')"); $id = mysql_insert_id(); mysql_quey("UPDATE `test2` SET `test_id` = $id WHERE `name` = 'Mark'");
SQL Server 2008 and newer have a MERGE statement which does exactly that.
See the MSDN Books Online docs on MERGE for details.
Basically, you need four things:
So you basically define something like:
MERGE (targettable) AS t
USING (sourcetable) AS s
ON (JOIN condition between s and t)
WHEN MATCHED THEN
UPDATE SET t.Col1 = s.Col1, t.Col2 = s.Col2 (etc.)
WHEN NOT MATCHED THEN
INSERT(Col1, Col2, ..., ColN) VALUES(s.Col1, s.Col2, ......, s.ColN)
This is done as one statement and highly optimized by SQL Server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With