What the MERGE statement does
MERGE compares a target table with a source table. In one statement, it inserts, updates or deletes rows in the target, based on how its rows match the source. It’s useful when a table needs to stay in step with a set of new data. MERGE has been in SQL Server since 2008. The syntax here is for SQL Server and Azure SQL. Azure Synapse Analytics and Fabric Data Warehouse have their own variant.
A basic example
Suppose dbo.Products holds current prices and dbo.PriceUpdates holds new ones. This statement updates the prices of products that exist in both tables, and inserts products that appear only in the updates:
MERGE INTO dbo.Products AS t
USING dbo.PriceUpdates AS s
ON t.ProductId = s.ProductId
WHEN MATCHED THEN
UPDATE SET t.Price = s.Price
WHEN NOT MATCHED BY TARGET THEN
INSERT (ProductId, Price)
VALUES (s.ProductId, s.Price);
Read it in parts:
INTO dbo.Products AS tnames the target, the table that changes.USING dbo.PriceUpdates AS snames the source. It can also be a derived table.ONsets how rows match. Compare columns from both tables. Don’t filter the target table inside ON, because the Microsoft documentation warns that this can return unexpected and incorrect results. Put that condition in a WHEN clause with AND instead.WHEN MATCHEDupdates target rows that match a source row.WHEN NOT MATCHED BY TARGETinserts a row for each source row that has no match in the target.
WHEN NOT MATCHED BY SOURCE covers target rows that have no match in the source. It can update or delete those rows. Use DELETE there with care, because it removes rows from the target.
Rules that cause errors
- End the statement with a semicolon. MERGE requires the semicolon terminator. Running it without one raises error 10713.
- Don’t change the same target row twice. If more than one source row matches a target row and the statement updates it, SQL Server returns an error. The statement also can’t update and delete the same row. Make the source unique on the match columns before the MERGE runs.
- Use one WHEN NOT MATCHED BY TARGET clause. The statement allows only one.
See what changed
Add an OUTPUT clause after the WHEN clauses to see which rows were inserted, updated or deleted. In the OUTPUT clause, $action returns INSERT, UPDATE or DELETE for each row. Microsoft recommends OUTPUT for counting rows by action. @@ROWCOUNT, read after the statement, returns only the total.
When you test a MERGE on real data, wrap it in a transaction first. If the counts look wrong, you can roll back.
When a plain UPDATE or INSERT is enough
If you’re only updating one table from another, Microsoft’s documentation suggests plain INSERT, UPDATE and DELETE statements for better performance and scalability. The same price update written as an UPDATE with a join looks like this:
UPDATE t
SET t.Price = s.Price
FROM dbo.Products AS t
INNER JOIN dbo.PriceUpdates AS s
ON t.ProductId = s.ProductId;
Use MERGE when one statement has to handle inserts, updates and deletes together.
Matching rows like a join
The ON condition in MERGE works like a join condition. When both tables share column names, qualify each column with its table alias. The same approach is covered in how to join two tables in SQL with the same column names.