Delete Duplicate Record from a table
- Declare a temporary table @table.
- Create two fields 'id' as int identity and 'data' as varchar.
- Insert four records with values 'not duplicate row', 'duplicate row' and 'duplicate row'.
- Create a while loop with a condition 1=1 and break the loop if zero row count found means no duplicate record exists in the table now.
- In the query we have used 'GROUP BY' and 'COUNT' aggregate function to calculate the duplicate records.
- 'GROUP BY' is used for grouping data values having count more than 1.
- Selecting MAX(id) means getting id of higher number.
- When while loop will run first time it will group three duplicate records with values 'duplicate row' and get its max id like here max id will be 4 and delete the fourth record.
- In WHILE loop it calculate the Row count by '@@Rowcount' but here row count is not equal to zero than again while loop will run and delete the third record and again row count not equal to zero.
- While loop run again but not able to get any duplicate row. This time row count will be 0 and it will break the loop and come out.
DECLARE @table TABLE(
id INT IDENTITY(1, 1)
, data VARCHAR(20)
)
INSERT INTO @table VALUES ('not duplicate row')
INSERT INTO @table VALUES ('duplicate row')
INSERT INTO @table VALUES ('duplicate row')
INSERT INTO @table VALUES ('duplicate row')
SELECT * FROM @table
WHILE 1 = 1
BEGIN
DELETE FROM @table
WHERE id IN (SELECT MAX(id)
FROM @table
GROUP BY data
HAVING COUNT(*) > 1)
IF @@Rowcount = 0
BREAK ;
END
SELECT * FROM @table
id INT IDENTITY(1, 1)
, data VARCHAR(20)
)
INSERT INTO @table VALUES ('not duplicate row')
INSERT INTO @table VALUES ('duplicate row')
INSERT INTO @table VALUES ('duplicate row')
INSERT INTO @table VALUES ('duplicate row')
SELECT * FROM @table
WHILE 1 = 1
BEGIN
DELETE FROM @table
WHERE id IN (SELECT MAX(id)
FROM @table
GROUP BY data
HAVING COUNT(*) > 1)
IF @@Rowcount = 0
BREAK ;
END
SELECT * FROM @table
No comments:
Post a Comment