Allen Buckley's Blog

My experiences in .Net

  Home  |   Contact  |   Syndication    |   Login
  7 Posts | 0 Stories | 12 Comments | 13 Trackbacks

News



Archives

Fav. Websites

I came across an error today in SQL Server 2005. 

 

The row value(s) updated or deleted either do not make the row unique or they alter multiple rows.

 

The reason I got this error was because I created a table for a data mapping application and in my haste I forgot to include a primary key.  I checked my table today and found records that were duplicated and whenever I tried to delete or edit a row in SQL Management Studio this error showed up.  So I knew I had to put a primary key in the database and in order to do that I had to take care of the duplication.  Below are 3 different ways I found to fix this problem.

 

Solution 1

The first solution is for getting rid of several duplicated rows.  This solution used multiple queries.

 

Step 1. 

     This query puts the duplicate keys in a separate table that this query creates.


SELECT col1, col2, col3=COUNT(*)

INTO HOLDKEY

FROM Table1

GROUP BY col1, col2

HAVING COUNT(*) > 1

 

Step 2. 

     This query creates another new table and just includes unique primary keys.

 

SELECT DISTINCT Table1.*

INTO HoldUps

FROM Table1, HoldKey

WHERE Table1.col1 = HoldKey.col1

AND Table1.col2 = HoldKey.col2

 

*Before step 3 check the HoldUps table for duplicates.  If you have duplicates in that table refer to the microsoft link below.

 

Step 3. 

     This query deletes the duplicate rows from the original table.

 

DELETE Table1

FROM Table1, HoldKey

WHERE Table1.col1 = HoldKey.col1

AND Table1.col2 = HoldKey.col2

 

Step 4. 

     This query inserts the unique rows from the Holdups table into the original table.

 

 INSERT Table1 SELECT * FROM Holdups

 

Step 5.

     Delete the two new tables that the queries created and you’re finished.

 

 

Solution 2

This solution is used for instances where you just need to delete one duplicate row.  Just use a delete statement like you would any time you delete a row.  The only difference is the SET ROWCOUNT 1 makes it so that only 1 row gets deleted.  Delete the 1 row and that takes care of the duplication.

 

SET ROWCOUNT 1

DELETE FROM Table1

WHERE col1 = ‘0001’

 

Solution 3

This solution is a yet another way to approach this problem.  In this approach you use the query below to create a new column that numbers your records 1, 2, 3, etc.  This will get rid of the duplication and allow you to delete the records or update them as you need.  After you fix the duplication problem, just remember to delete the column.

 

ALTER TABLE Table1

ADD TempID int IDENTITY(1, 1)

 

 

 

 

The microsoft link I found that led me to solution 1.

http://support.microsoft.com/default.aspx?scid=kb;en-us;139444

posted on Thursday, July 27, 2006 4:33 PM

Feedback

# re: The row value(s) updated or deleted either do not make the row unique or they alter multiple rows. 8/10/2006 11:07 PM lbr
Hello.
I've also came across this problem in the similar situation...
Seems very strange for me because database must have internal row_number.
By the way mySQL, pgSQL handles this situation without any problems (PHPpgAdmin, PHPmyAdmin).
Maybe you know why MS SQL Management Studio bahaves like this ?
plz answer to my e-mail: lbr3 _(at)_ inbox.lv

# re: The row value(s) updated or deleted either do not make the row unique or they alter multiple rows. 6/3/2007 3:53 PM Lawrence Botley
Hi,

If didnt care about the data in the table you could:

truncate table tablename

You can then set the key

cheers

Lawrence

# Authoritative Pages Error - an item with the same key has already been added 6/18/2007 11:25 PM Jim @ imason


# Authoritative Pages Error - an item with the same key has already been added 6/19/2007 12:01 AM Mirrored Feeds
I came across an issue tonight after we had inadvertently added duplicate Authoriative Pages to our SharePoint

# re: The row value(s) updated or deleted either do not make the row unique or they alter multiple rows. 3/27/2008 9:31 AM Jenn
Thanks a ton- I ran across this today. Instead of a muttering and griping for a half hour, it took me a five minute search. A toast to you!

# re: The row value(s) updated or deleted either do not make the row unique or they alter multiple rows. 5/22/2008 9:46 AM E
Helped really!

Thanks!!!

# re: The row value(s) updated or deleted either do not make the row unique or they alter multiple rows. 8/28/2008 10:41 AM mm
Thank you! You are a prince among men.

# Cheers 9/4/2008 6:03 AM M L
Seems a pretty weird issue, considering the error. Removing a duplicate makes it LESS unique? Lunacy.

I hadn't resorted to using a manual DELETE yet, so cheers for confirming next move.

# re: The row value(s) updated or deleted either do not make the row unique or they alter multiple rows. 9/17/2008 12:58 PM CA
Ahh I had been doing Solution 1 when I ran into this problem, but solution 3 will save me a lot of headache!


Thanks.

# re: The row value(s) updated or deleted either do not make the row unique or they alter multiple rows. 9/25/2008 5:17 AM Paul
Thanks! Solution 3 was a doddle

# re: The row value(s) updated or deleted either do not make the row unique or they alter multiple rows. 10/30/2008 12:09 PM Nandip
Thanks. Very useful article. Solved my problem as well.

# re: The row value(s) updated or deleted either do not make the row unique or they alter multiple rows. 10/30/2008 2:57 PM Brian
Thanks! You are the man!

# re: The row value(s) updated or deleted either do not make the row unique or they alter multiple rows. 10/30/2008 6:55 PM paulina
I almost freak out today when I saw this problem..... Thanks you are going to heaven !!!

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 7 and 7 and type the answer here: