Tuesday, April 17, 2007

Sql Server, remove duplicate records

Sometimes I need to remove duplicates from a table, given a particular column to be checked.

This few lines of transact-sql code will help, I hope:

CREATE TABLE #tmp_tableCleanDup (id int, email varchar(200))
CREATE UNIQUE CLUSTERED INDEX pk ON #tmp_tableCleanDup(ID)
CREATE UNIQUE INDEX removeduplicates on #tmp_tableCleanDup (email) WITH IGNORE_DUP_KEY

BEGIN TRANSACTION

 INSERT #tmp_tableCleanDup
 SELECT e.ID, e.email
 FROM OriginalTable e

 DELETE OriginalTable
 WHERE 1=1
 AND id NOT IN (SELECT id FROM  #tmp_tableCleanDup)

COMMIT TRANSACTION

 DROP TABLE #tmp_tableCleanDup

No comments: