MSSQLMSSQL 2627
MSSQL 2627: Unique Constraint Violation
SQL Server error 2627 is thrown when an INSERT or UPDATE would create a duplicate value in a PRIMARY KEY or UNIQUE constraint. It is similar to error 2601 (duplicate key in unique index).
Example Query
INSERT INTO users (email) VALUES ('bob@example.com');
-- Fails if that email is already in the tableCommon Causes
- 1Inserting a row with an explicit primary key value that already exists
- 2UNIQUE constraint on email or username violated by duplicate data
- 3Parallel inserts racing to insert the same value
How to Fix It
Use MERGE for upsert logic. Check for existence first: IF NOT EXISTS (SELECT 1 FROM users WHERE email = 'x') INSERT …. Use TRY/CATCH in T-SQL to handle the error and retry or update instead.
Need a reliable database?
Try Supabase — free PostgreSQL with a generous free tier. No credit card required.
Got a query causing this error?
Paste it into SQLbuddy and get an instant plain-English explanation with optimization tips.
Analyze My Query →