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
How to Insert multiple rows with a single insert statement?
when you create a database how is it stored? : Sql server database administration
What are the authentication modes in sql server? How can it be changed?
Can you explain important index characteristics?
Explain datetime2 data type in sal server 2008?
Why do we backup Active Directory ?
How to delete duplicate rows in sql server?
How much space does sql server 2016 take?
What are different types of statements that are supported by sql?
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?
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?
What kind of problems occurs if we do not implement proper locking strategy?
What is collation sensitivity?
What are the tool windows in sql server management studio? : sql server management studio
What stored by the msdb?