microsoft sql merge code example
Example 1: merge clause with inner join
MERGE table1
USING (SELECT table3.keycolumn,
table2.DataColumn1,
table2.DataColumn2
FROM table2
INNER JOIN table3
ON table2.anotherKey = table3.anotherKey
WHERE table2.anotherKey = 'A1') tmpTable
ON
table1.keyColumn = tmpTable.keyColumn
WHEN MATCHED THEN
UPDATE
SET table1.DataColumn1 = tmpTable.DataColumn1
,table1.DataColumn2 = tmpTable.DataColumn2;
Example 2: sql merge statement
MERGE LoginTypes T
USING (SELECT 'System' as Description) S
ON(S.Description = T.Description)
WHEN NOT MATCHED BY TARGET
THEN INSERT(Description, CreatedTimestamp, LastUpdatedTimestamp)
VALUES('System', getdate(), getdate());