Wednesday, 11 September 2013

Format in TSQL of simple 'Merge into' query using 'Case'

Format in TSQL of simple 'Merge into' query using 'Case'

I have a TSQL UPDATE query as follows:
UPDATE Table
SET Value = CASE
WHEN (Condition1 = 'A') AND (Condition2 = '1') THEN 'BLAH-1'
WHEN (Condition1 = 'B') AND (Condition2 = '1') THEN 'BLAH-2'
END
WHERE (Condition1 IN ('A','B')) AND (Condition2 IN ('1'))
Can someone show me the format for the above as a 'MERGE INTO' query, if
it is possible?
MERGE INTO Table
USING ...
WHEN MATCHED THEN UPDATE SET ...
WHEN NOT MATCHED THEN INSERT ... VALUES ...
In other words, if (Condition1) AND (Condition2) don't exist, then add the
records to the table.
If it helps, the various 'cases' will either all need to be UPDATED or all
INSERTED.
I am using SQL server 2012.
Thanks in advance.

No comments:

Post a Comment