Does SQL Server Offer Anything Like MySQL's ON DUPLICATE KEY UPDATE
There's no DUPLICATE KEY UPDATE equivalent, but MERGE and WHEN MATCHED might work for you
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
MERGEINTO MyBigDB.dbo.METER_DATA WITH (HOLDLOCK) AS targetUSING (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 = 56112WHEN NOT MATCHED THEN INSERT (rtu_id, meter_id, meter_reading, time_local) VALUES (77748, '12B096876', 56112, '20150602 00:20:11');
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')