MERGE Statement
MERGE compares a source with a target and inserts, updates or deletes rows in one statement — ideal for syncing imported data.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Step-by-step screens
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Nov 26, 2026
Related tutorials
Learning objectives#
- Write a MERGE with all three actions
- Log what happened with OUTPUT $action
- Avoid duplicate-match errors
- Know when separate statements are better
Explanation#
MERGE target USING source ON match condition, then WHEN MATCHED (update or delete), WHEN NOT MATCHED BY TARGET (insert) and WHEN NOT MATCHED BY SOURCE (rows in the target that are missing from the source). The statement must end with a semicolon.
The source must have at most one row per target row; otherwise MERGE fails with “attempted to UPDATE or DELETE the same row more than once”. Deduplicate the source first.
OUTPUT $action, inserted.*, deleted.* reports exactly what changed — useful for audit logs.
Under concurrent load, add WITH (HOLDLOCK) on the target to avoid race conditions, or use separate UPDATE and INSERT statements in a transaction, which many teams find easier to read and tune.
Step-by-step screens#
Drawings of SQL Server 2022 Setup and SQL Server Management Studio (SSMS 20) with the ShopDB sample database. The red numbers match the numbered actions under each picture; names on your computer will differ.
Loading pictures…
Code example: Sync product prices from a supplier file#
-- Staging.Products was loaded from the supplier's CSV
MERGE dbo.Products WITH (HOLDLOCK) AS t
USING Staging.Products AS s
ON t.Sku = s.Sku
WHEN MATCHED AND (t.Price <> s.Price OR t.Name <> s.Name) THEN
UPDATE SET t.Price = s.Price, t.Name = s.Name
WHEN NOT MATCHED BY TARGET THEN
INSERT (Sku, Name, Price) VALUES (s.Sku, s.Name, s.Price)
WHEN NOT MATCHED BY SOURCE AND t.IsActive = 1 THEN
UPDATE SET t.IsActive = 0
OUTPUT $action AS Action, inserted.Sku, deleted.Price AS OldPrice, inserted.Price AS NewPrice;
Action Sku OldPrice NewPrice ------- ------- -------- -------- UPDATE KB-101 1499.00 1399.00 INSERT MS-220 NULL 799.00 UPDATE HD-009 2499.00 2499.00 -- deactivated (IsActive = 0)
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Rows are matched on Sku.
- Matched rows are updated only if something changed.
- New SKUs are inserted.
- Products the supplier dropped are deactivated, not deleted.
- OUTPUT lists every change with old and new prices.
Real-world use case#
A nightly job loads the supplier’s price list into a staging table and one MERGE brings the catalogue up to date, writing the OUTPUT into an audit table.
Common mistakes#
MERGE fails when one target row matches several source rows. Use ROW_NUMBER() or GROUP BY to deduplicate.
MERGE must be terminated with ; or you get a syntax error.
Best practices#
- Deduplicate the source
- Only update when values differ
- Use HOLDLOCK under concurrency
- Log changes with OUTPUT
Remember this#
MATCHED → update, NOT MATCHED BY TARGET → insert, BY SOURCE → delete/deactivate.
End with ;
OUTPUT $action shows what happened.
Practice exercise#
Your task: Write the clause that inserts source rows that do not exist in the target (Sku, Name).
Show solution
WHEN NOT MATCHED BY TARGET THEN
INSERT (Sku, Name) VALUES (s.Sku, s.Name)
Mini quiz#
Which clause handles target rows that are missing from the source?
Want more? Practise all SQL Server MCQs · SQL Server interview questions
My notes#
Saved in this browser.