Solved

Rebuild index results in duplicate key violation

Posted on 2016-08-29
9
53 Views
Last Modified: 2016-09-01
This is SQL 2005. I ran the Rebuild Index routine and it errors on one of the databases. The error is a violation of the primary key on a table. Somehow there is a duplicate record in the table. How do I find which records are duplicates and rebuild the index?
0
Comment
Question by:rwheeler23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 250 total points
ID: 41775808
You can use below example to find out duplicate values..

SELECT
    id, orgName
FROM (
     SELECT
         orgName, id,
         ROW_NUMBER() OVER (PARTITION BY orgName ORDER BY id) rnk
     FROM organizations
) AS d
WHERE rnk> 1

Let me know if you need more info here !
0
 
LVL 41

Accepted Solution

by:
Sharath earned 250 total points
ID: 41776916
You can simply group on key columns and check if there are any duplicates.
SELECT pk_col1, pk_col2 -- list all your pk_columns
       ,COUNT(*) cnt
  FROM your_table
 GROUP  BY pk_col1, pk_col2 -- list all your pk_columns
HAVING COUNT(*) > 1

Open in new window

0
 

Author Comment

by:rwheeler23
ID: 41779040
There are twelve databases with this table. I have run both of these on all databases and no results are returned. I am going to run rebuild index on each database individually.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41779277
Great ! let us know in case of any issue.
0
 

Author Comment

by:rwheeler23
ID: 41779516
Fortunately the rebuild index task tells you then record causing the issue. It happened in only one database and there were actually 6 duplicate records. Once I removed the duplicates the rebuild task finished without error. Very odd that duplicate records were allowed in the first place since they violated the primary key.
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41779524
Great ! Please go ahead and close the question and mark one or more comments as accepted solution. !!

Keep Learning.
0
 

Author Comment

by:rwheeler23
ID: 41779759
Thanks for your help.
0
 

Author Comment

by:rwheeler23
ID: 41779762
Both solutions were equally helpful.
0
 

Author Closing Comment

by:rwheeler23
ID: 41779763
Thanks for your help.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question