Skip to content
Microsoft SQL Server

SQL Server Error 2627

Violation of PRIMARY KEY or UNIQUE KEY constraint (cannot insert duplicate key)

Applies to: All supported SQL Server versions (2016 to 2025), Azure SQL Database, Azure SQL Managed Instance
Last technically reviewed 23 October 2026 on SQL Server 2025 (17.0.1145.4)
The full message
Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint 'PK_Customers'. Cannot insert duplicate key in object 'dbo.Customers'. The duplicate key value is (1).
Other forms of this message
UNIQUE constraint
Msg 2627, Level 14, State 1, Line 1
Violation of UNIQUE KEY constraint 'UQ_Customers_email'. Cannot insert duplicate key in object 'dbo.Customers'. The duplicate key value is (sara@example.com).
Followed by (always)
The statement has been terminated.
Error number
2627
Severity
14 (user-correctable error)
State
1
Product
Microsoft SQL Server Database Engine
Versions
All supported SQL Server versions (2016 to 2025), Azure SQL Database, Azure SQL Managed Instance
Last reviewed
23 October 2026

What it means

Error 2627 means you tried to store a key value that already exists in a column protected by a PRIMARY KEY or UNIQUE constraint, so SQL Server rejected the row.

A PRIMARY KEY or UNIQUE constraint guarantees that no two rows share the same key. When an INSERT, UPDATE or MERGE would create a second row with an existing key, SQL Server stops the statement, rolls it back and raises 2627. The message tells you the constraint, the table and the exact duplicate value, which is the fastest clue.

Severity 14 means a normal, catchable error in the data you sent, not a server problem. A UNIQUE index created with CREATE UNIQUE INDEX raises the closely related error 2601 instead; the causes and fixes are the same.

Common causes

  • The same row is inserted twice: a double-clicked submit button, a retried request or a script that is run again.
  • The application supplies the key value itself and picks one that is already taken, instead of letting an IDENTITY column or a SEQUENCE generate it.
  • An UPDATE changes a key column to a value another row already has.
  • An INSERT ... SELECT or a bulk load brings in a source that contains duplicates, either among its own rows or against rows already in the table.

Diagnose it

Read-only These queries only read metadata and dynamic management views. They do not change data, permissions or configuration, so they are safe to run first.

Which columns does the constraint cover?

SELECT kc.name AS constraint_name, kc.type_desc, c.name AS column_name, ic.key_ordinal
FROM sys.key_constraints AS kc
JOIN sys.index_columns AS ic ON ic.object_id = kc.parent_object_id AND ic.index_id = kc.unique_index_id
JOIN sys.columns AS c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
WHERE kc.name = N'PK_Customers'
ORDER BY ic.key_ordinal;
What to look for

The key columns in order. A multi-column key is duplicated only when every listed column matches.

Find the row that already holds the key

SELECT *
FROM dbo.Customers
WHERE id = 1;  -- the duplicate key value from the message
What to look for

The existing row. Compare it with what you were inserting to decide whether to skip, update or reject the new data.

Find duplicates in a staging source before loading it

SELECT id, COUNT(*) AS copies
FROM staging.Customers
GROUP BY id
HAVING COUNT(*) > 1;

-- and keys that already exist in the target
SELECT s.id
FROM staging.Customers AS s
WHERE EXISTS (SELECT 1 FROM dbo.Customers AS c WHERE c.id = s.id);
What to look for

Any rows returned would raise 2627 when loaded.

How to fix it

Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.

Insert only when the key is not there yet

Changes data

For double submits and re-runs, guard the INSERT. Under concurrency, two sessions can both pass the check; keep the constraint as the final safety net and handle 2627 in your code, or take an update lock in the check as shown.

BEGIN TRANSACTION;
IF NOT EXISTS (SELECT 1 FROM dbo.Customers WITH (UPDLOCK, HOLDLOCK) WHERE id = 1)
    INSERT INTO dbo.Customers (id, name) VALUES (1, N'Sara');
COMMIT TRANSACTION;

Update the existing row instead (upsert)

Changes data

When a matching key should update the row rather than fail, update first and insert only if nothing was updated. This pattern is easier to reason about than MERGE.

BEGIN TRANSACTION;
UPDATE dbo.Customers WITH (UPDLOCK, SERIALIZABLE)
SET name = N'Sara'
WHERE id = 1;
IF @@ROWCOUNT = 0
    INSERT INTO dbo.Customers (id, name) VALUES (1, N'Sara');
COMMIT TRANSACTION;

Let SQL Server generate the key

Changes schema

Most duplicate-id errors come from the application choosing ids. For new tables, use an IDENTITY column (or a SEQUENCE) and stop sending the id. Changing an existing column to IDENTITY needs a table rebuild, so plan it as a migration.

CREATE TABLE dbo.Customers2 (
    id   INT IDENTITY(1,1) CONSTRAINT PK_Customers2 PRIMARY KEY,
    name NVARCHAR(100) NOT NULL
);

INSERT INTO dbo.Customers2 (name) VALUES (N'Sara');  -- no id supplied

Remove duplicates from the source before loading

Changes data

Keep one row per key with ROW_NUMBER() and skip keys that already exist in the target. Decide with the data owner which duplicate should win; the ORDER BY below keeps the most recently updated one.

INSERT INTO dbo.Customers (id, name)
SELECT id, name
FROM (
    SELECT id, name,
           ROW_NUMBER() OVER (PARTITION BY id ORDER BY updated_at DESC) AS rn
    FROM staging.Customers
) AS s
WHERE s.rn = 1
  AND NOT EXISTS (SELECT 1 FROM dbo.Customers AS c WHERE c.id = s.id);

Examples

Inserting a duplicate primary key

CREATE TABLE dbo.Customers (
    id   INT CONSTRAINT PK_Customers PRIMARY KEY,
    name NVARCHAR(100)
);

INSERT INTO dbo.Customers (id, name) VALUES (1, N'Sara');
INSERT INTO dbo.Customers (id, name) VALUES (1, N'Ali');  -- id 1 already exists
Result
Msg 2627, Level 14, State 1, Line 7
Violation of PRIMARY KEY constraint 'PK_Customers'. Cannot insert duplicate key in object 'dbo.Customers'. The duplicate key value is (1).
The statement has been terminated.

Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.

An UPDATE that creates a duplicate

CREATE TABLE dbo.Customers (
    id   INT CONSTRAINT PK_Customers PRIMARY KEY,
    name NVARCHAR(100)
);
INSERT INTO dbo.Customers (id, name) VALUES (1, N'Sara'), (2, N'Ali');

UPDATE dbo.Customers SET id = 1 WHERE id = 2;  -- 1 is taken
Result
Msg 2627, Level 14, State 1, Line 7
Violation of PRIMARY KEY constraint 'PK_Customers'. Cannot insert duplicate key in object 'dbo.Customers'. The duplicate key value is (1).
The statement has been terminated.

Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.

A UNIQUE constraint on an email column

CREATE TABLE dbo.Customers (
    id    INT IDENTITY PRIMARY KEY,
    email NVARCHAR(200) CONSTRAINT UQ_Customers_email UNIQUE
);

INSERT INTO dbo.Customers (email) VALUES (N'sara@example.com');
INSERT INTO dbo.Customers (email) VALUES (N'sara@example.com');
Result
Msg 2627, Level 14, State 1, Line 7
Violation of UNIQUE KEY constraint 'UQ_Customers_email'. Cannot insert duplicate key in object 'dbo.Customers'. The duplicate key value is (sara@example.com).
The statement has been terminated.

Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.

How to prevent it

Use an IDENTITY column or a SEQUENCE for surrogate keys so the database, not the application, guarantees uniqueness. Make writes that might run twice idempotent (check first, or upsert), and de-duplicate staging data before loading it.

Keep the constraint even when the application checks for duplicates: under concurrency only the constraint is reliable. Handle 2627 (and 2601) in your error handling so a duplicate becomes a clear message to the user rather than a crash. For key design, see primary key vs foreign key.

Common questions

How do I fix SQL Server error 2627?

Find the existing row with the duplicate key value shown in the message, then decide: skip the insert, update the existing row instead, or let SQL Server generate the key with IDENTITY so the application stops choosing ids.

What is the difference between error 2627 and 2601?

Error 2627 is raised by a PRIMARY KEY or UNIQUE constraint; error 2601 by a unique index created with CREATE UNIQUE INDEX. Both mean a duplicate value was rejected and are fixed the same way.

Can I just turn the constraint off to load my data?

No. Disabling or dropping the key lets duplicates in, and re-enabling it later fails until they are removed. Clean the data before loading instead.

Learn more on CodeWithSQL

Official documentation

Last technically reviewed 23 October 2026; examples and diagnostics run on SQL Server 2025 (17.0.1145.4). Message text checked against the engine's sys.messages catalogue.

Still stuck on a SQL Server error?

Browse the full SQL Server error library, or get hands-on help from an experienced SQL professional.