How to retrieve duplicate rows in a table?
How to delete the duplicate entries in a table?

Answer Posted / mohit d jethva

///////////Cursor for delete duplicate record in
table//////////

DECLARE @ID int
DECLARE @NAME NVARCHAR(50)
DECLARE @COUNT int

DECLARE CUR_DELETE CURSOR FOR
SELECT [ID],[NAME],COUNT([ID]) FROM [Example] GROUP BY [ID],
[NAME] HAVING COUNT([ID]) > 1

OPEN CUR_DELETE

FETCH NEXT FROM CUR_DELETE INTO @ID,@NAME,@COUNT
/* Loop through cursor for remaining ID */
WHILE @@FETCH_STATUS = 0
BEGIN

DELETE TOP(@COUNT -1) FROM [Example] WHERE ID = @ID

FETCH NEXT FROM CUR_DELETE INTO @ID,@NAME,@COUNT
END

CLOSE CUR_DELETE
DEALLOCATE CUR_DELETE

Mohit D Jethva

Is This Answer Correct ?    7 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to Insert multiple rows with a single insert statement?

549


when you create a database how is it stored? : Sql server database administration

494


What are the authentication modes in sql server? How can it be changed?

602


Can you explain important index characteristics?

527


Explain datetime2 data type in sal server 2008?

549






Why do we backup Active Directory ?

611


How to delete duplicate rows in sql server?

577


How much space does sql server 2016 take?

568


What are different types of statements that are supported by sql?

594


You are designing a strategy for synchronizing an SQL Azure database and multiple remote Microsoft SQL Server 2008 databases. The SQL Azure database contains many tables that have circular foreign key relationships?

99


1)what is the difference between Reinitializing a Subscription and synchronization the subscription? 2)when to use reinitializing ? 3)when to use synchronization? 4)when adding table or deleting a table what to do?(reinz.. or syn) 5)when adding a column what to do?

1542


What kind of problems occurs if we do not implement proper locking strategy?

967


What is collation sensitivity?

551


What are the tool windows in sql server management studio? : sql server management studio

624


What stored by the msdb?

585