Allen Buckley's Blog
My experiences in .Net

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

Thursday, July 27, 2006 4:33 PM

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


Feedback

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

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 8/10/2006 11:07 PM | lbr

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

Hi,

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

truncate table tablename

You can then set the key

cheers

Lawrence 6/3/2007 3:53 PM | Lawrence Botley

# Authoritative Pages Error - an item with the same key has already been added

I came across an issue tonight after we had inadvertently added duplicate Authoriative Pages to our SharePoint 6/19/2007 12:01 AM | Mirrored Feeds

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

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! 3/27/2008 9:31 AM | Jenn

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

Helped really!

Thanks!!! 5/22/2008 9:46 AM | E

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

Thank you! You are a prince among men. 8/28/2008 10:41 AM | mm

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

Ahh I had been doing Solution 1 when I ran into this problem, but solution 3 will save me a lot of headache!


Thanks. 9/17/2008 12:58 PM | CA

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

Thanks! Solution 3 was a doddle 9/25/2008 5:17 AM | Paul

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

Thanks. Very useful article. Solved my problem as well. 10/30/2008 12:09 PM | Nandip

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

Thanks! You are the man! 10/30/2008 2:57 PM | Brian

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

I almost freak out today when I saw this problem..... Thanks you are going to heaven !!! 10/30/2008 6:55 PM | paulina

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

the solution #3
is sweet... 2/24/2009 5:44 PM | triak

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

Solution 3 worked great!

When I was done deleted the duplicate records I used:
ALTER TABLE Table1
DROP COLUMN TempID

to get rid of the extra column. 3/19/2009 2:17 PM | Tim

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

Very nice Solution, thanks. I used the first solution and solved my problem 4/11/2009 5:01 PM | yadugna

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

Very nice Solution, thanks. I used the first solution and solved my problem.
4/11/2009 5:01 PM | yadugna

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

great work, it worked perfectly
thank you so muchh 7/12/2009 5:52 AM | hamed

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

great work!

Solution_2 was quite easy to use - even for me a SQL-noobie ;)

ty! 8/24/2009 10:01 PM | vyva

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

Thanks for the quick solution. Solution 2 worked for me, but don't forget to set the rowcount back to 0 if you're continuing to work, otherwise everything else you do will be impacted by the rowcount change. 9/24/2009 3:17 PM | Bryan

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

3 solution worked.....
thanks alot.............. 10/3/2009 8:45 AM | Ismail

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

I got this error too. Could somebody tell me why this error happened? Why can't you have duplicate rows in a table??? GRRRRR 10/27/2009 9:56 AM | Hennie7863

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

Good Job zanbi on sol 3 3/19/2010 5:49 PM | Zahid Hussain

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

Thanks so much! Used Solution 3 - It saved me! 3/23/2010 1:29 AM | henry

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

Life saver! Thank you very much for solution 3!! :D 4/7/2010 4:32 AM | Sam

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

For once a blog that works!! 4/28/2010 7:21 PM | Darren Cavanagh

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

Great solutions! I used solution 2. Thank you! 7/8/2010 1:26 PM | J.

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

Thank you for adding this! Your advece is really nice. It is exactly what I was looking for. I couldn't cope with it by myself...I couldn't get rid of the duplication Now I am pleased cause I followed your advice. 7/16/2010 7:52 AM | Music Search

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

Many thanks, Alter table worked 7/16/2010 12:43 PM | M Dev

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

A lot thanks for these codes and also for the step by step guide. 8/4/2010 12:58 AM | Car Accident Solicitor

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

thanks a lot for this post. i made it as my guide in learning mySQL 8/24/2010 8:39 PM | Metropolitan Collection Service

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

A lot of thanks for sharing this codes. This could truly help a lot of folks out there. 8/31/2010 9:26 AM | Blair Oliver Scott

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


Thank you for another great article. Where else could anyone get
that kind of information in such a perfect way of writing?
I have a presentation next week, and I am on the look for such information. 9/1/2010 8:48 AM | Equidebt .

Post a comment