Finding and Deleting Duplicate records in SQL Server

So, I have a situation where I have some duplicate records in a SQL Server.  This is a result of a bad decision I made about 3 months ago.  That's another story.  I needed to find out how to find those records in my database easily.

http://blogs.techrepublic.com.com/datacenter/?p=372

 Here's the crux:

 

SELECT * | DELETE FROM table

WHERE uniquefield IN

(SELECT uniquefield

FROM table

WHERE EXISTS(

SELECT NULL

FROM table AS tmptable

WHERE table.field1 = tmptable.field1

[AND table.field2 = tmptable.field2

[AND ...]]

HAVING table.uniquefield > MIN(tmptable.uniquefield)

)

)

So, I'm running it on my database.  It's taking a while since I have close to 2 million rows in the table that I'm trying to match.  Crossing my fingers.  :)  
 

2 Comments

Comments have been disabled for this content.