I need to execute a query as shown below. Columns KEY1 and KEY2 are keys that cannot be repeated. If there are keys matching, I need to perform an update VAL instead of insert. How can I do that in Sql Server?
INSERT INTO tableA
(
KEY1,
KEY2,
VAL,
) VALUES (
-- Row A
'datakeyA1',
'datakeyA2',
'somevaluetoinsertorupdate'
) , (
-- Row B
'datakeyB1',
'datakeyB2',
'somevaluetoinsertorupdate'
) , (
-- Row C
'datakeyC1',
'datakeyC2',
'somevaluetoinsertorupdate'
);
I tried using MERGE, but looking at the syntax, I am not sure if it supports updating / inserting multiple rows. If anyone has encountered a similar situation in the past, could you please help out?
EDIT:
If I were using MySql, I would have just used:
ON DUPLICATE KEY UPDATE
VAL = VALUES(VAL)
in the query.
Bulk insert / UpdateWHEN NOT MATCHED THEN INSERT (KEY1,KEY2,VAL) VALUES ('datakeyA1', 'datakeyA1', 'datakeyA1'), ('datakeyB1', 'datakeyB1', 'datakeyB1');. This seems to be an invalid syntax.