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

MysqlSql ServerTsql

Mysql Problem Overview


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?

Mysql Solutions


Solution 1 - Mysql

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');

Solution 2 - Mysql

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

[Inserting, Updating, and Deleting Data by Using MERGE][1]

[1]: http://msdn.microsoft.com/en-us/library/bb522522(v=sql.100).aspx "Inserting, Updating, and Deleting Data by Using MERGE"

Solution 3 - Mysql

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') 

Solution 4 - Mysql

SQL Server 2008 has this feature, as part of TSQL.
See documentation on MERGE statement here - http://msdn.microsoft.com/en-us/library/bb510625.aspx

Solution 5 - Mysql

SQL server 2000 onwards has a concept of instead of triggers, which can accomplish the wanted functionality - although there will be a nasty trigger hiding behind the scenes.

Check the section "Insert or update?"

http://msdn.microsoft.com/en-us/library/aa224818(SQL.80).aspx

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBen GriswoldView Question on Stackoverflow
Solution 1 - MysqlIvanDView Answer on Stackoverflow
Solution 2 - Mysqlmichael pearsonView Answer on Stackoverflow
Solution 3 - MysqlmesutukView Answer on Stackoverflow
Solution 4 - MysqlshahkalpeshView Answer on Stackoverflow
Solution 5 - MysqlTetraneutronView Answer on Stackoverflow