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
Which table keeps the locking information?
How to loop through the result set with @@fetch_status?
Explain aggregate functions?
How to insert multiple rows with a subquery?
What happens if time-only values are provided as date and time literals?
What are audit control procedures?
What is unpivot?
you have a table with close to 100 million records recently, a huge amount of this data was updated now, various queries against this table have slowed down considerably what is the quickest option to remedy the situation? : Sql server administration
Why do we use sql limitations? Which constraints can we use while making a database in sql?
Do you know what is difference between index seek vs. Index scan?
Differentiate between mongodb vs. Sql server?
What is read committed?
How can I change procedure name in sql server?
Can group functions be used in the order by clause in ms sql server?
Do you know what guidelines should be followed to help minimize deadlocks?