Categories
mysql sql-server tsql

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?

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

Inserting, Updating, and Deleting Data by Using MERGE