Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Rebuild index results in duplicate key violation

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

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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

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 35

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

877 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