Violation of … constraint ‘…’. Cannot insert duplicate key in object ‘…’ (Msg 2627)

Hi, I am getting the error message

Violation of … constraint ‘…’. Cannot insert duplicate key in object ‘…’ (Msg 2627)

when I try to insert records into a table using MERGE command.

Actually, the target table which I try to insert records is empty & does not have any records now.

Please tell me why I am getting this error message as no existing records are there, there by preventing duplicate insertion.

The source data itself must have the duplicates. Find out why

1 Like

Either the source data has duplicates - or the MERGE is causing the same row to be inserted/updated multiple times. This can be caused by an invalid join criteria or missing criteria.

Just run the source query and see where there might be duplicates

Thanks for the insight. The source query was returning duplicate records. I will use DISTINCT clause & see the output.