Sep
10

How to delete duplicate rows when there is no primary key in SQL Server

SQL          Trackback

I have to admit that this issue was killing me few weeks ago. Some folks gave me table with no primary key and with some duplicate rows. When I tried to update data in that table SQL Server didn’t know which record to update and all I was getting was error message. First thing I tried was to add TOP(1) after DELETE:

DELETE TOP(1) FROM MyTable WHERE MyColumn = 'something'

but unfortunatelly that didn’t work.

After almost hour of searching I found this solution:

SET ROWCOUNT 1
DELETE
FROM MyTable
WHERE (MyColumn = 'something')

and that worked for me. I hope it will save you some time.


No Comments

Make A Comment

No comments yet.

Comments RSS Feed    TrackBack URL

Leave a comment

top