Monday, December 8, 2008

Delete Duplicate Records(MS SQL SERVER )

Below code is useful to delete duplicate records. The table must have identity column, which will be used to identify the duplicate records. NameOftheTable in example is has ID as Identity Column and Columns which have duplicate data are DuplicateValueColumn1, DuplicateValueColumn2 and DuplicateValueColumn3.

Query

DELETE FROM NameOftheTable WHERE ID NOT IN ( SELECT MAX(ID) FROM NameOftheTable GROUP BY DuplicatevalueColumn1, DuplicateValueColumn2, DuplicateValueColumn2)

No comments: