On this page
Why it happens
Error 2627 means you tried to write a row whose key value already exists in a column protected by a PRIMARY KEY or UNIQUE constraint. Those constraints exist precisely to reject duplicates, so SQL Server stops the statement and rolls it back rather than store two rows with the same key.
The message names the constraint, the table, and the exact duplicate value: The duplicate key value is (1). That value is your fastest clue: it tells you which row already exists. The error is severity 14, which is a normal, catchable constraint violation, not a server problem.
Common triggers are inserting a record twice (a double submit or a re-run script), supplying an explicit id that is already taken, an UPDATE that changes a key into one another row already holds, or an INSERT ... SELECT whose source contains duplicates.
Examples
Inserting a duplicate primary key
CREATE TABLE dbo.Customers (
id INT PRIMARY KEY,
name NVARCHAR(100)
);
INSERT INTO dbo.Customers (id, name) VALUES (1, 'Sara');
INSERT INTO dbo.Customers (id, name) VALUES (1, 'Ali'); -- id 1 already exists
Msg 2627, Level 14, State 1, Line 8 Violation of PRIMARY KEY constraint 'PK_Customers'. Cannot insert duplicate key in object 'dbo.Customers'. The duplicate key value is (1).
An UPDATE that creates a duplicate
-- Two rows exist with id 1 and id 2.
-- Changing id 2 to 1 collides with the existing row.
UPDATE dbo.Customers SET id = 1 WHERE id = 2;
Msg 2627, Level 14, State 1, Line 3 Violation of PRIMARY KEY constraint 'PK_Customers'. The duplicate key value is (1).
Duplicates inside an INSERT ... SELECT
INSERT INTO dbo.Customers (id, name)
SELECT id, name FROM staging.Customers; -- staging has two rows with id 5
Msg 2627, Level 14, State 1, Line 1 Violation of PRIMARY KEY constraint 'PK_Customers'. The duplicate key value is (5).
How to fix it
Insert only if the key does not already exist
Guard the INSERT so it runs only when the key is free. This is the simplest fix for double-submit and re-run scenarios.
IF NOT EXISTS (SELECT 1 FROM dbo.Customers WHERE id = 1)
INSERT INTO dbo.Customers (id, name) VALUES (1, 'Sara');
Use MERGE to insert or update (upsert)
If a matching key should update the existing row instead of failing, use MERGE so one statement handles both cases.
MERGE dbo.Customers AS t
USING (SELECT 1 AS id, 'Sara' AS name) AS s
ON t.id = s.id
WHEN MATCHED THEN UPDATE SET t.name = s.name
WHEN NOT MATCHED THEN INSERT (id, name) VALUES (s.id, s.name);
Let the database generate the key
Most duplicate-id errors come from supplying the id by hand. Make the column an IDENTITY (or use a SEQUENCE) and stop passing a value, so every row gets a fresh number.
CREATE TABLE dbo.Customers (
id INT IDENTITY(1,1) PRIMARY KEY,
name NVARCHAR(100)
);
-- Do not pass id; SQL Server assigns it
INSERT INTO dbo.Customers (name) VALUES ('Sara');
De-duplicate the source before loading
When bulk loading, remove duplicates in the source first. ROW_NUMBER() keeps one row per key.
INSERT INTO dbo.Customers (id, name)
SELECT id, name
FROM (
SELECT id, name,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY name) AS rn
FROM staging.Customers
) d
WHERE rn = 1;
How to prevent it
Use an IDENTITY column or a SEQUENCE for surrogate primary keys so the database, not the application, guarantees uniqueness. This removes the most common cause of 2627 outright.
Make writes idempotent: use IF NOT EXISTS or MERGE for inserts that might run twice, and validate keys in the application before submitting. De-duplicate any staging or import data before loading it. For details on choosing keys, see primary key vs foreign key and the constraints guide.
Common questions
How do I fix SQL Server error 2627?
Stop inserting a key that already exists. Either guard the INSERT with IF NOT EXISTS, use MERGE to update instead of duplicating, or make the key an IDENTITY column so SQL Server assigns unique values automatically. The message tells you the duplicate value to look for.
What is the difference between error 2627 and 2601?
They are almost the same problem. Error 2627 is a violation of a PRIMARY KEY or UNIQUE constraint, while error 2601 is a violation of a UNIQUE INDEX. Both mean you tried to insert a duplicate value into something that must be unique, and the fixes are identical.
Why does error 2627 happen on an UPDATE?
An UPDATE that changes a key column to a value another row already holds also creates a duplicate, so the same unique constraint rejects it.