Solved

Rebuild index results in duplicate key violation

Posted on 2016-08-29
9
44 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
  • 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 40

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQl Agent job fails--SSIS package looses password 6 46
Help with SQL joins 9 48
Two tables - Sum of values - What is the difference 31 51
Help Required 2 29
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

805 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