dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Home/Error Library/sqlserver-duplicate-key-unique-index
SQL Server
SQL Server 2025 / 2022Data Integrity & Constraints
7 min read
Violation of UNIQUE KEY constraint / Cannot insert duplicate key in object

SQL Server "Cannot insert duplicate key row" — Error 2601 or 2627?

Differences between SQL Server Error 2601 (unique index) and 2627 (unique constraint/PK), race conditions in IF NOT EXISTS, idempotent TRY/CATCH, MERGE WITH (HOLDLOCK), and IGNORE_DUP_KEY pitfalls.

Reviewed by:Senior Database Reliability Engineer
Tested Versions: SQL Server 2025 (17.0 CU10 KB5125471) & 2022 (16.0 CU14)

Operational Verification & Reliability Metadata

Last Technical Review: 29 Ekim 2026
Tested VersionsSQL Server 2025 (17.0 CU10 KB5125471) & 2022 (16.0 CU14)
Required PrivilegesINSERT and transaction execution privileges
Risk Assessment
MediumIGNORE_DUP_KEY silently drops duplicate rows during bulk inserts, concealing data loss
Expected OutputDeterministic idempotent insertion without race conditions or swallowed errors
Rollback Procedure:Standard transactional rollback under TRY/CATCH blocks
Microsoft Learn: SQL Server Unique Indexes & Error 2601/2627
First 60 Seconds Emergency Protocol:

The error string is a symptom, not the root cause. Do not restart the server blindly. Run the diagnostic queries below to pinpoint the actual failure mode.

▸Symptoms

plaintext
Violation of UNIQUE KEY constraint 'UQ_Kullanici_Email'. Cannot insert duplicate key in object 'dbo.Kullanici'. The duplicate key value is (a@b.com).
  • •Error 2627: Primary Key or Unique Constraint violation.
  • •Error 2601: Unique Index violation.

▸Correct Resolution: Idempotent Exception Handling

sql
BEGIN TRY INSERT INTO dbo.Kullanici (Email, Ad) VALUES (@e, @ad); END TRY BEGIN CATCH IF ERROR_NUMBER() IN (2601, 2627) SELECT * FROM dbo.Kullanici WHERE Email = @e; ELSE THROW; END CATCH

Other Database Error Triage Playbooks

Other battle-tested diagnostic guides for high-severity database errors.

All Errors (50)