I used the following code to update or Insert record from one table to another but the insert statement doesn't skip when duplicate record. How can i bypass that?
I get the following error. I want to skip duplicate record and insert the new
Msg 2627, Level 14, State 1, Line 6 Violation of PRIMARY KEY constraint 'PK_MIBOMD_KEY_0'. Cannot insert duplicate key in object 'dbo.MIBOMD'. The duplicate key value is ( , 0, 1). The statement has been terminated.
MERGE [MIBOMD] AS TARGET
USING [whl] AS SOURCE
ON (TARGET.[bomItem] = SOURCE.ItemID AND TARGET.bomEntry = SOURCE.bomEntry AND TARGET.bomRev = SOURCE.rev)
WHEN MATCHED AND TARGET.bomEntry = SOURCE.bomEntry
AND TARGET.partId <> SOURCE.partid THEN
UPDATE SET TARGET.partId = SOURCE.partid
WHEN NOT MATCHED BY TARGET THEN
INSERT (partId,bomEntry, srcLoc, qty, lead)
VALUES (SOURCE.partid, SOURCE.bomEntry, 'R14SDS', SOURCE.qty, SOURCE.lead);