Write a query to delete duplicate records in SQL SERVER
Answer Posted / sumit
I have The Same Problem And I Have Done Woth This
DECLARE @empid int, @empname varchar(50),@Cnt int
DECLARE duplicate_cursor CURSOR FOR
-- select all columns in table bit you must have an count column
select empid,empname, count(*) Cnt
from tbl_Temp
group by empid, empname
Having count(*) > 1
OPEN duplicate_cursor
FETCH NEXT FROM duplicate_cursor
INTO @empid, @empname,@Cnt
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Cnt = @Cnt - 1
SET ROWCOUNT @Cnt
DELETE tbl_Temp
WHERE @empid = empid AND @empname = empname
FETCH NEXT FROM duplicate_cursor
INTO @empid, @empname
END
CLOSE duplicate_cursor
DEALLOCATE duplicate_cursor
-- dont forget to set rowcount to 0
SET ROWCOUNT 0
| Is This Answer Correct ? | 10 Yes | 6 No |
Post New Answer View All Answers
How to delete multiple rows with one delete statement in ms sql server?
Why the trigger fires multiple times in single login?
What is a DBMS, query, SQL?
What security features are available for stored procedure?
How to create a simple stored procedure in ms sql server?
What are transactions in sql?
What were the latest updates to SQL Azure service?
How many databases instances are there in sql server 2000?
What are the properties and different types of sub-queries?
Can we insert data into a view?
Why should you use or avoid select * statements?
What is the difference between functions and stored procedures?
How do I setup a sql server database?
What is the stuff?
what is the sql equivaent of the dataset relation object ?