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

Advertisements

Rebuilding Indexes using the SSMS Database Maintenance Wizard

Usually I do all 3 job under one single task but with separate schedule for each of them:

 

Reorganize_Index

Rebuild_Index

Update_Statistics

 

Details can be found here:

https://www.red-gate.com/simple-talk/sql/database-administration/rebuilding-indexes-using-the-ssms-database-maintenance-wizard/