How to Fix SQL Server Error 2512 (Msg 2512)
Duplicate keys on a data or index page trigger sql server error 2512 during checks. You fix this error by rebuilding the bad index or restoring from a clean backup. This problem carries a severity level of 16 in the database engine. It points to table corruption on a specific object, index, partition, and page. When two rows share a unique key on one page, the engine stops the check. Teams must act fast to protect data and keep apps running. What causes sql server error 2512 in the database
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
Duplicate keys on a data or index page trigger sql server error 2512 during checks. You fix this error by rebuilding the bad index or restoring from a clean backup.
This problem carries a severity level of 16 in the database engine. It points to table corruption on a specific object, index, partition, and page. When two rows share a unique key on one page, the engine stops the check. Teams must act fast to protect data and keep apps running.
What causes sql server error 2512 in the database
The error message reports a table error with duplicate keys on a specific page. It lists the object ID, index ID, partition ID, and allocation unit ID. This means a unique index or primary key holds two identical values on one page.
Disk sub-system failures are the most common root cause for this page corruption. A bad disk controller or faulty memory module can write wrong bytes to storage. Power loss without battery backup on cache drives also scrambles page data during writes.
Sometimes, a bug in the database engine or storage driver causes the duplicate entry. Mixing sort orders or changing character sets without rebuilding indexes can also break key rules. Whatever the cause, the index structure no longer matches the rules of the table.
How to diagnose and fix the error safely in production
Start by reading the full error log to find the exact object ID and index ID. Check which index ID is listed in the message before you plan any repair. If the index ID is greater than one, the flaw sits in a non-clustered index.
You can fix a broken non-clustered index by dropping and creating it again. This action builds fresh pages from the clean base table data without losing any rows. Run this step during a low traffic window so you do not block active users.
Kubernetes API Server Access + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
If the index ID is zero or one, the base table data itself has corruption. Before removing duplicate rows, inspect the conflicting records using a read-only account. Members of the fixed reader role can read all data from user tables and views. Never grant the fixed owner role to engineers who only need to inspect broken rows. Members of that owner role can perform all maintenance tasks and even drop the database.
Once you find the duplicate keys in the base table, check your clean backups first. Restoring the damaged page or database from a known good backup is the safest fix. If you have no clean backup, copy the valid rows into a new table. Then remove the extra bad row so you can build the unique key again.
Preventing future page and index corruption
After you fix the table, run a full check on the whole database right away. Make sure no other pages or indexes suffered damage from the same hardware event. Check the system event logs to see if a disk or memory part failed.
Move the database files to healthy storage if you find any hardware errors in logs. Schedule regular integrity checks every week so you catch page errors before backups expire. Keep your storage firmware and database engine patched to avoid known write bugs.
Limit who can run manual table repairs on your production servers. Use separate database users for daily reading and high-risk maintenance work. A database user is the identity of a login when it connects to a database. Strict role rules stop accidental table changes while your team fixes index errors.
Add runtime guardrails beside SQL Server
Role grants and network rules are needed setup for SQL Server. Still, an allowed user or agent tool can send a bad delete or read raw customer fields over a valid session. In practice, hoop.dev sits inline in the TDS protocol beside the database as a sidecar for your SQL Server. For every query an agent or human runs in production, hoop.dev decides whether to pass, mask, block, or escalate it for review.
With hoop.dev SQL Server controls, rules refuse banned statements before they run, hold risky queries for human review, and mask matched sensitive values in supported result columns in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for HIPAA, PCI DSS, SOX, and GDPR audits. See the source on GitHub.
Frequently asked questions
Does error 2512 mean I will lose data?
Not always. If the error hits a non-clustered index, you can rebuild it without data loss. If it hits the base table, you only lose data if you lack backups.
What does severity 16 mean for this error?
Severity 16 means the error is a user or data problem that admins can fix. It does not crash the whole server, but it blocks operations on the damaged page.
How do I know which table has the duplicate keys?
Look at the object ID number shown in the error 2512 message text. Query the system catalog views in that database to match the object ID to a table name.
Related guides
In this post