Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Rebuild index results in duplicate key violation

Posted on 2016-08-29
9
Medium Priority
?
62 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 30

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 1000 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

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 30

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

715 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