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



  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

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.

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.

Good for you to identify the errors about this. 10/13/2010 2:04 AM | BLS

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

This is very use full for me 11/2/2010 7:16 AM | Jyotsna

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

Thank you! Caught the issue early on and used solution #2. 11/12/2010 1:32 PM | kpd

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

Wow. Thanks for taking the time to publish this. Really helped me out. Solution #2 did the job. 11/23/2010 8:34 AM | Paul Godfrey

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

I have tried it at home. Thanks for the info. Its very useful to me. 1/7/2011 9:06 AM | Torrent

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

how can i delete a value in a particular row of a table....not all values 2/3/2011 3:26 AM | sanjay

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

#3 was excellent solution. Thanks!! 3/10/2011 2:45 PM | dw

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

Very informative and useful article indeed. I really like the way writer has presented his views. I hope to see more great articles in future as well. 3/24/2011 4:38 AM | internet marketing product revie

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

Definitely a great post. Hats off to you! The information that you have provided is very helpful. 3/26/2011 8:02 AM | magkatarr

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

i love this blog 3/29/2011 1:51 PM | vattkoppor barn

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

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.
3/31/2011 12:51 AM | Headsets Microphones

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

I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.
3/31/2011 7:05 PM | debt management advice

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

I know it's good if I want to make a note from the post. 4/5/2011 1:55 AM | Online Jobs

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

THanks for posting this solution, its just saved me lots of time... 4/8/2011 7:37 AM | Music Search

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

Awesome stuff you guys got here. I really like the theme of the website and how well you organized the content. It's a marvelous job I will come back and check you out sometime.
4/11/2011 10:41 PM | whiplash symptoms

# cheap coach handbags

re: The row value(s) updated or deleted either do not make the row unique or they alter multiple rows. 4/14/2011 5:02 AM | cheap coach handbags

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

That will make your comments shorter and you will resort to the aforementioned method, spamming the living resume writer
daylights out of someone’s work. The final thought : abbreviate, just not too much. 4/18/2011 7:41 AM | sidakala

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

well, i had not been into programming as i don't like to spend one whole day for an error of (,) 4/29/2011 3:40 AM | watch movies online

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

I stumbled upon it is an informative and interesting post. I think it is incredibly useful and knowledgeable. Thank you for the efforts you have made in writing this article. I'm hoping identical best product by you later on as well. The truth is your creative writing ability has inspired me. 5/10/2011 1:57 AM | Internet Marketing Review

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

I have been looking for this information for a couple of this for my thesis project,.thanks for sharing buddy 5/20/2011 5:38 PM | malleoloc

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

Option 2 worked like a dream! Thanks so much! 6/16/2011 12:00 PM | Michelle

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


I just found your site and wanted to say that I have really enjoyed browsing your posts.In any case I'll be subscribing to your blog and I hope you post again soon
8/2/2011 1:20 AM | android developers

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

This was very useful. Had to use one of these techniques for the second time this week. 8/31/2011 5:14 PM | webfesto

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

This truly touches my interest deeply. I by no means had visited a type of weblog which has the description of your website. The internet style actually say's the whole topic of the blogger. Thanks for sharing this impressing post. 9/12/2011 8:11 PM | aktiv kapital

# Perfect!

Brilliant! You saved me a headache. I went for solution 3 and it worked great. :) 9/20/2011 6:09 AM | Justin

# Mr

I’ve ended up visiting your current web site for a little bit now and I also often locate a gemstone with your brand new discussions. Nice one for sharing! 10/11/2011 1:08 AM | apex credit management

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

Substantially, the usefulness of this information has its significant. 10/26/2011 6:55 PM | whiplash compensation

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

Studying this information So i happy to show that I have an incredibly just right uncanny feeling I found out just what I needed. 10/31/2011 1:36 PM | Jual Mobil

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

Studying this information Jual Mobil
So i happy to show that I have an incredibly just right uncanny feeling I found out just what I needed. 10/31/2011 1:42 PM | Jual Mobil

# Jual Rumah Murah

It is a very informative and useful post thanks it is good material to read this post increases my knowledge. 11/4/2011 12:40 AM | meonglanang95

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

One thing I have noticed is that there are plenty of myths regarding the lenders intentions whenever talking about foreclosure. 11/7/2011 12:47 AM | Rumah Dijual

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

I never had visited a rare blog like this. I am very glad to read an interesting article
with lots of knowledge shared by the blogger. I will be looking forward to this blog
for my future reference. Thanks for posting. 11/14/2011 7:41 PM | Pocket Hole Jig

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

I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post. 11/18/2011 1:44 AM | wholesale dropship

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

Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift. 11/18/2011 1:45 AM | lowongan kerja

# Mr

Thanks, I’ve recently been seeking information about this subject for a long time and yours is the best I have discovered.
12/9/2011 1:22 AM | tequila bar NYC

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

You have a great way with words as shown in your article. You’re even good with informational content like you have here. I like your views and your writing technique. Keep up the good work! 12/11/2011 8:46 PM | wholesale dropship products

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

Spot on with this write-up, I truly think this website needs much more consideration. 12/11/2011 8:52 PM | wholesale dropship shoes

# Mr

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me. 12/13/2011 6:06 PM | 732-993-4272

# Mr

It is great to have the opportunity to read a good quality article with useful information on topics that plenty are interested on. The points that the data stated are all first hand on actual experiences even help more. Go on doing what you do as we enjoy reading your work. 12/14/2011 9:41 PM | back pain

# Mr

Cool! That was a great tournament. I love the show. I just want to say congratulations to the winner. 12/18/2011 9:32 PM | Roman Vintfeld

# Mr

If you want to see the mind blowing article with real facts and figures, this has really tremendous impacts on readers and I admire the writing skill of the author.
12/19/2011 10:17 PM | best margaritas nyc

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

Thanks for sharing this, I used solution 2 for a quick fix. Nice one! 1/10/2012 4:44 AM | Fil

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

Very significant article for us, I think the representation of this article is actually superb one. This is my first visit to your site. Thanks a lot and keep sharing the information. Keep updating the information for all of us. 1/16/2012 12:24 AM | Psychic

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

Thank you for this really informative article. 1/23/2012 5:21 PM | RM

Post a comment