Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SQL Server Offer Anything Like MySQL's ON DUPLICATE KEY UPDATE

In MySQL, if you specify ON DUPLICATE KEY UPDATE and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have identical effect:

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE c=c+1;

UPDATE table SET c=c+1 WHERE a=1;

I don't believe I've come across anything of the like in T-SQL. Does SQL Server offer anything comparable to MySQL's ON DUPLICATE KEY UPDATE?

like image 885
Ben Griswold Avatar asked Jul 29 '09 02:07

Ben Griswold


People also ask

What does on duplicate key update do?

ON DUPLICATE KEY UPDATE is a MariaDB/MySQL extension to the INSERT statement that, if it finds a duplicate unique or primary key, will instead perform an UPDATE. The row/s affected value is reported as 1 if a row is inserted, and 2 if a row is updated, unless the API's CLIENT_FOUND_ROWS flag is set.

How do I use insert on duplicate key update?

Syntax : INSERT INTO table (column_names) VALUES (values) ON DUPLICATE KEY UPDATE col1 = val1, col2 = val2 ; Along with the INSERT statement, ON DUPLICATE KEY UPDATE statement defines a list of column & value assignments in case of duplicate.

What is on duplicate key?

The Insert on Duplicate Key Update statement is the extension of the INSERT statement in MySQL. When we specify the ON DUPLICATE KEY UPDATE clause in a SQL statement and a row would cause duplicate error value in a UNIQUE or PRIMARY KEY index column, then updation of the existing row occurs.

What is duplicate key in SQL Server?

If you specify an ON DUPLICATE KEY UPDATE clause and a row to be inserted would cause a duplicate value in a UNIQUE index or PRIMARY KEY , an UPDATE of the old row occurs.


3 Answers

There's no DUPLICATE KEY UPDATE equivalent, but MERGE and WHEN MATCHED might work for you

Inserting, Updating, and Deleting Data by Using MERGE

like image 151
michael pearson Avatar answered Sep 28 '22 23:09

michael pearson


I was surprised that none of the answers on this page contained an example of an actual query, so here you go:

A more complex example of inserting data and then handling duplicate

MERGE INTO MyBigDB.dbo.METER_DATA WITH (HOLDLOCK) AS target USING (SELECT     77748 AS rtu_id    ,'12B096876' AS meter_id    ,56112 AS meter_reading    ,'20150602 00:20:11' AS local_time) AS source (rtu_id, meter_id, meter_reading, time_local) ON (target.rtu_id = source.rtu_id   AND target.time_local = source.time_local) WHEN MATCHED   THEN UPDATE       SET meter_id = '12B096876'          ,meter_reading = 56112 WHEN NOT MATCHED   THEN INSERT (rtu_id, meter_id, meter_reading, time_local)       VALUES (77748, '12B096876', 56112, '20150602 00:20:11'); 
like image 45
IvanD Avatar answered Sep 28 '22 21:09

IvanD


You can try the other way around. It does the same thing more or less.

UPDATE tablename 
SET    field1 = 'Test1', 
       field2 = 'Test2' 
WHERE  id = 1 

IF @@ROWCOUNT = 0 
  INSERT INTO tablename 
              (id, 
               field1, 
               field2) 
  VALUES      (1, 
               'Test1', 
               'Test2') 
like image 24
mesutuk Avatar answered Sep 28 '22 21:09

mesutuk