Here is script to delete duplicate records in table:
DECLARE @Count int
DECLARE @FirstName nvarchar(50)
DECLARE @LastName nvarchar(50)
DECLARE dublicate_cursor CURSOR FAST_FORWARD FOR
SELECT FirstName, LastName, Count(*) - 1 FROM Users
GROUP BY FirstName, LastName
HAVING Count(*) > 1
OPEN dublicate_cursor
FETCH NEXT FROM dublicate_cursor INTO @FirstName, @LastName, @Count
WHILE @@FETCH_STATUS = 0
BEGIN
DELETE TOP(@Count) FROM Users WHERE FirstName = @FirstName AND LastName = @LastName
FETCH NEXT FROM dublicate_cursor INTO @FirstName, @LastName, @Count
END
CLOSE dublicate_cursor
DEALLOCATE dublicate_cursor
Please let know if you face any issue in this.
No comments:
Post a Comment