Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: Deleting duplicate records in SQL Server

I have an sql server database, that I pre-loaded with a ton of rows of data.

Unfortunately, there is no primary key in the database, and there is now duplicate information in the table. I'm not concerned about there not being a primary key, but i am concerned about there being duplicates in the database...

Any thoughts? (Forgive me for being an sql server newb)

like image 800
rockit Avatar asked Nov 20 '09 19:11

rockit


1 Answers

Well, this is one reason why you should have a primary key on the table. What version of SQL Server? For SQL Server 2005 and above:

;WITH r AS
(
    SELECT col1, col2, col3, -- whatever columns make a "unique" row
    rn = ROW_NUMBER() OVER (PARTITION BY col1, col2, col3 ORDER BY col1)
    FROM dbo.SomeTable
)
DELETE r WHERE rn > 1;

Then, so you don't have to do this again tomorrow, and the next day, and the day after that, declare a primary key on the table.

like image 91
Aaron Bertrand Avatar answered Sep 22 '22 00:09

Aaron Bertrand