A Merge
statement does not make the target table a copy of the source table, You've got that part wrong.
It provides a shorter way to write conditional DML statements - insert
/ update
/ delete
- by letting you write a single statement that will execute either one (or any combination of) the above DML statements.
However, even though Merge
is written as a single statement, behind the sceens SQL Server actually execute it as a series of statements - each part of it gets it's one statement - and this can cause plenty of problems for the unexpecting user.
Aaron bertrand have published an article about this entitled Use Caution with SQL Server's MERGE Statement - you should read it thoroughly before using the Merge
statement in SQL Server.
Most of the time, Merge
is used to perform an "upsert" - meaning - update the record(s) if exists, or insert if not. A better, safer pattern for "upsert" in SQL server is described in Aaron's answer to this SO question, And I highely recommend using that over Merge
any time it's possible.