Capstone: reconcile a feed with a deduplicated MERGE
Build a deterministic source with a ranking CTE, update changed target rows, insert missing keys, and audit each action through `OUTPUT $action`.
What you are trying to do
A nightly feed contains duplicate versions of a business key. Reconcile it into a local temporary target and inspect the exact actions. The sample writes only to a session-local temporary table and drops it at the end.
Work through it
- Read the target seed rows and source versions. Source key A appears twice, with versions 1 and 2.
- In
RankedSource, the version and source ID form a deterministic order. Only row number one for each key reaches MERGE. - Trace the match predicate. Changed key A takes the UPDATE branch, missing key C takes the INSERT branch, and target-only key B remains unchanged because no delete branch exists.
- Read the OUTPUT rows as an action audit.
$actionreports the operation for each affected target row. The MERGE statement ends with a semicolon. - Change the source to contain two rows with the same key and identical version. Explain why the source ordering needs a unique tie-breaker before relying on a single winner.
- Review Microsoft's MERGE syntax, OUTPUT behavior, and concurrency cautions before adapting this pattern to a permanent target. MERGE · OUTPUT clause
Check your work
The action audit contains one UPDATE for key A and one INSERT for key C. Key B remains unchanged. The final target has three rows, and all writes were confined to a local temporary table that is dropped by the script.
Practice SQL
Open in SQL FlowIF OBJECT_ID('tempdb..#LabTarget') IS NOT NULL DROP TABLE #LabTarget;
CREATE TABLE #LabTarget (BusinessKey char(1) NOT NULL PRIMARY KEY, Description nvarchar(40) NOT NULL);
INSERT INTO #LabTarget (BusinessKey, Description) VALUES ('A', N'Old A'), ('B', N'Keep B');
DECLARE @Action TABLE (ActionName nvarchar(10), BusinessKey char(1), OldDescription nvarchar(40), NewDescription nvarchar(40));
;WITH RankedSource AS (
SELECT BusinessKey, Description, SourceVersion, SourceId,
ROW_NUMBER() OVER (PARTITION BY BusinessKey ORDER BY SourceVersion DESC, SourceId DESC) AS SourceRank
FROM (VALUES
('A', N'New A', 2, 20),
('A', N'Stale A', 1, 10),
('C', N'New C', 1, 30)
) AS s(BusinessKey, Description, SourceVersion, SourceId)
)
MERGE #LabTarget AS target
USING (SELECT BusinessKey, Description FROM RankedSource WHERE SourceRank = 1) AS source
ON source.BusinessKey = target.BusinessKey
WHEN MATCHED AND target.Description <> source.Description THEN
UPDATE SET Description = source.Description
WHEN NOT MATCHED BY TARGET THEN
INSERT (BusinessKey, Description) VALUES (source.BusinessKey, source.Description)
OUTPUT $action, inserted.BusinessKey, deleted.Description, inserted.Description
INTO @Action (ActionName, BusinessKey, OldDescription, NewDescription);
SELECT ActionName, BusinessKey, OldDescription, NewDescription FROM @Action ORDER BY BusinessKey;
SELECT BusinessKey, Description FROM #LabTarget ORDER BY BusinessKey;
DROP TABLE #LabTarget;