I just recently seen a cool way to delete, insert, or update data using a merge statement (even though it has been done since SQL Server 2008).
Don’t mind keys, duplicates, and such; let’s just keep it simple. Say we have two tables, and one is used to alter the other.

We could go (or some variation of it):

Or, with a MERGE:

A few things to note here:
1. [TARGET] and [SOURCE] are just aliases; those can be named whatever you please.
2. You can match on more than just 1 field
Pretty dope!
Link to Microsoft’s MERGE page
Link to OUTPUT Clause page

 

(Originally posted on Medium)

Back To Top