How to Fix SQL Server Error 1505 (Msg 1505)
You get sql server error 1505 when you try to build a unique index on a table that already holds duplicate values. To fix it, you must find and remove or merge those duplicate rows before you run the index step again. This problem often stops schema updates and app deployments right in their tracks. When a migration script adds a unique rule, the database checks every existing row first. If two rows share the same key value, the engine cancels the task right away. What causes sql server error
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.
You get sql server error 1505 when you try to build a unique index on a table that already holds duplicate values. To fix it, you must find and remove or merge those duplicate rows before you run the index step again.
This problem often stops schema updates and app deployments right in their tracks. When a migration script adds a unique rule, the database checks every existing row first. If two rows share the same key value, the engine cancels the task right away.
What causes sql server error 1505 in your database
The database engine returns message 1505 at severity level 16. The message says the unique index creation stopped because the engine found a duplicate key for your object and index name. Severity 16 means the error comes from user data or query rules, not a server crash.
Duplicate rows enter tables in a few common ways. An app might send two write requests at the exact same time before a unique rule exists. Data imports from flat files or staging tables can also bring in repeated records. Even trailing spaces or case settings can make two strings look identical to the sorting engine.
When you ask the database to create a unique index or primary key, it scans the target columns. It builds a temporary sort table to order the keys. As soon as two matching keys touch in that sort, the engine aborts the whole index build and rolls back the change.
How to find and fix duplicate keys in production
Start by reading the exact error text from your deployment log. The message tells you the table name, the index name, and the duplicate key value that caused the stop. That single key value gives you a fast starting point, though more duplicates may hide in the table.
To check the full table safely, use an account with read-only permissions. In SQL Server, members of the db_datareader fixed role can read all data from user tables and views without changing anything. Run a read query that groups by the index columns and counts rows with a total greater than one.
Kubernetes API Server Access + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
When you check user tables, be careful with sensitive fields like emails, phone numbers, card numbers, or national IDs. Good data masking rewrites matched fields in results so analysts can spot duplicate keys without exposing raw personal strings on screen.
Once you have the list of repeated keys, talk with your product or data team before deleting rows. Sometimes you need to keep the newest row and drop older copies. Other times, child tables point to both row IDs, so you must update those foreign keys first.
Safe ways to apply the fix and add the index
Never grant broad admin roles just to clean up a few bad rows. The db_owner role can run all maintenance tasks and can even drop the whole database. Instead, use a scoped role like db_datawriter, which lets a user add, delete, or change data in user tables during the cleanup window.
Run your row updates and deletes inside a small transaction during low traffic hours. Check the row count before you commit the change to make sure you only touched the bad duplicates. After the table is clean, run your index build script again so the database enforces uniqueness for all future writes.
If your app keeps sending duplicate rows while you work, fix the app logic first. Add a check in the code or use an upsert pattern so new duplicates do not slip in right before your index finishes building.
Govern SQL Server queries and responses in flight
Table grants decide which SQL Server objects a caller may touch. They do not check query intent or rewrite sensitive values in results across every client and agent. By design, hoop.dev sits inline in the TDS wire protocol next to SQL Server as a sidecar for your SQL Server. On every production batch from an engineer or AI agent, hoop.dev decides to pass, mask, block, or hold it for approval.
With hoop.dev guardrails and hoop.dev data masking, banned statements are refused or held for review, matched sensitive fields in supported result columns are masked in memory, and each decision is logged. Check out the code on GitHub.
Frequently asked questions
What does severity 16 mean for sql server error 1505?
Severity 16 means the problem is a general error that the user or developer can fix. The database engine is healthy, and the failure only affects the current index statement because of duplicate data in the table.
Can I ignore duplicate keys instead of deleting rows?
No, you cannot build a unique index while duplicate keys remain in the target columns. If your business rules allow repeated values in that column, you must create a standard non-unique index instead of a unique one.
Why did my query find no duplicates when error 1505 just fired?
This usually happens when your index includes multiple columns or when collation rules treat uppercase and lowercase letters as the same value. Make sure your check query groups by all column in the index definition and matches the table collation.
Related guides
In this post