Violation of PRIMARY KEY constraint

Scenario: Occurs when trying to insert a record with a duplicate value in a column that has a PRIMARY KEY constraint.

Error Message: Native Error Code: 2627 SQL State: 23000 [Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY KEY constraint. Cannot insert duplicate key in object 'dbo.XYZ'. The duplicate key value is (XZZ).

Cause: The record already exists in the database, or the key field is not unique in the source data.

Resolution:

  • Check if the record already exists by using a query (SELECT * FROM [Table] WHERE [PrimaryKeyColumn] = 'Value').
  • Ensure the source data generates unique keys (e.g., combining fields or using GUIDs).