Sunday, June 8, 2008

Delete Duplicate records SQL SERVER 2005

Make a Copy of your table first

Problem


Solution

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)

)

)