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 do I know if localdb is running?
What is the difference between varchar and nvarchar?
How to manipulate data from one table to another table ?
What is bulkcopy in sql?
What are system databases into sql server (2005/2008)?
Is sql server difficult to learn?
How do you start single user mode in clustered installations?
Why we need to use secondry database file? though, we can do same work using primary database file also.
What is a cursor, index in sql?
Name some of the open source software that you can use in alternative to SSR?
What are key constraints?
Why does sql studio use a single registered database repository? : sql server management studio
Define normalisation?
Some queries related to SQL
What are the characteristics of modern DBMS?