Delete duplicate records in SQL Server?

Use this script at you own risk. I am not responsible for any accidental mass amount of your data get deleted. Always use begin tran and rollback tran to make sure you know what are you deleting.

 

 

BEGIN TRAN

DELETE x

FROM (  SELECT *, rn=row_number() over   (   partition by  [DELETED]               ,[NETBIMDER]      ,[PG_DEPT]      ,[EMAILNOTIFICATION]    order by ID  )  from ND_LOG ) xwhere rn > 1

ROLLBACK TRAN

 

 

TO see what will be deleted, use this:

SELECT *SELECT *FROM (  SELECT *, rn=row_number() over   (   partition by  [DELETED]               ,[NETBIMDER]      ,[PG_DEPT]      ,[EMAILNOTIFICATION]    order by ID  )  from ND_LOG ) xwhere rn > 1

 

ID = Primary key.

7If you don’t have a primary key, you can use ORDER BY (SELECT NULL)

 

Reff: https://stackoverflow.com/questions/3317433/delete-duplicate-records-in-sql-server