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
Sql studio em braces a variety of capabilities, but will I need them all? Is there a simpler product ? : sql server management studio
What is database replication? What are the different types of replication you can set up in sql server?
Mention the 3 ways to get a count of the number of records in a table.
Why SQL Agent is used?
Can we create clustered index on composite key?
What is the recommended total size of your memory optimized tables?
What are the different normalization forms?
What is data modeling and Reterminal integrity?
what are the reporting service components in SSRS?
What security features are available for stored procedure?
What is attribute? : sql server analysis services, ssas
How to insert a new row into a table with "insert into" statements in ms sql server?
What are sub reports?
How to execute function in stored procedure sql server?
What is the osql utility?