I have this query which syncs the target table with the source table. Missing rows in the target need to be inserted, changed rows (only number and name) need to be updated, and missing rows in the source need to be deleted from the target table:
MERGE Table1 AS Target
USING Table2 AS Source
ON Source.id_no = Target.Id
AND Source.number = Target.Number
AND Source.[name] = Target.[Name]
WHEN NOT MATCHED BY Target THEN
INSERT (Id, Number, [Name])
VALUES (Source.id_no, Source.number, Source.[name])
WHEN MATCHED THEN UPDATE SET
Target.Number = Source.number,
Target.[Name] = Source.[name]
WHEN NOT MATCHED BY Source THEN
DELETE
But the above is always updating ALL rows every time the query is executed.
What am I doing wrong?