🗄️ MERGE Was Supposed to Update, and It Inserted a Duplicate Instead
A `MERGE` statement written to “update if it exists, insert if it doesn’t” can end up inserting duplicate rows for keys that genuinely already exist – not because `MERGE` itself is buggy, but because the join condition used to match source and target rows doesn’t uniquely identify a row (matching on a non-unique or nullable column), so a row that SHOULD match an existing target row fails to match cleanly and falls through to the INSERT branch instead.
🔎 The Problem
MERGE INTO Customers AS target
USING StagingCustomers AS source
ON target.Email = source.Email -- Email allows NULLs and isn'"'"'t unique
WHEN MATCHED THEN
UPDATE SET target.Name = source.Name
WHEN NOT MATCHED THEN
INSERT (Email, Name) VALUES (source.Email, source.Name);
-- A staging row with a NULL Email (or an Email that exists but with
-- different casing, if the column collation is case-sensitive) never
-- satisfies the ON condition against an existing target row - SQL Server
-- correctly falls through to WHEN NOT MATCHED and inserts a "new" row
-- that'"'"'s actually a duplicate of an existing customer.
✅ Fix: Match on a Genuinely Unique, Non-Nullable Key
- Join `MERGE`’s `ON` clause on the table’s actual primary key or a guaranteed-unique business key – never on a column that permits NULLs or duplicates, since `MERGE` (like any join) simply won’t match rows the way you intend if the matching column itself doesn’t uniquely identify one.
- For a source of truth that genuinely lacks a unique natural key, adding an explicit surrogate key to the staging table (populated during an earlier ETL step) before the `MERGE` runs is worth the extra step – it turns an ambiguous match into a guaranteed one.
- Auditing `MERGE` statements specifically for NULL-safe matching is worth doing as a standing check – `target.Email = source.Email` is false (not true) when both sides are NULL, so two “matching” NULL rows still fall through to INSERT unless the condition explicitly accounts for that with something like `(target.Email = source.Email OR (target.Email IS NULL AND source.Email IS NULL))`.
⚠️ Why This Slips Past Initial Testing
- A test dataset built with clean, always-present email addresses can run through `MERGE` correctly every single time – the duplicate-insert behavior only appears once real-world data includes the NULLs, casing differences, or trailing whitespace that a hand-crafted test dataset conveniently avoids.
- This is also a well-known source of subtler `MERGE` bugs beyond duplicates – Microsoft’s own documentation notes several edge cases (including a documented issue with `MERGE` and triggers under certain conditions) worth being aware of before relying on it for anything business-critical without careful testing against realistic data.
MERGE matches rows exactly the way you told it to, not the way you meant – and a non-unique join condition means ‘exists already’ and ‘looks new’ can both be true for the same row.

