Deleting Duplicate Records from a Table

October 9, 2007

– SQL Server 2005 

–1.Using CTE with RowNum function

 –2. Using Set RowCount Command

SELECT * FROM dbo.duplicateTest

SET ROWCOUNT 1

DELETE FROM dbo.duplicateTest WHERE ID = 1

SET ROWCOUNT 0

SELECT * FROM dbo.duplicateTest

 –3. Using Top Command

SELECT * FROM dbo.duplicateTest

DELETE TOP(1) FROM dbo.duplicateTest WHERE ID = 1

SELECT * FROM dbo.duplicateTest

 –4. 

DELETE TOP (SELECT COUNT(*) -1 FROM dbo.Emptest WHERE EmpID = ‘Basharat’) FROM dbo.Emptest  WHERE EmpID = ‘Basharat’

 –5. Using Duplicate table and by renaming it ( Work in both Sql2000 & SQL 2005 )


Follow

Get every new post delivered to your Inbox.