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
Difference between Sql server reporting services and Crystal reports?
How to create new tables with "select ... Into" statements in ms sql server?
What are indexers?
Do you know what is user defined datatypes and when you should go for them?
What is the openxml statement in sql server?
What is the default schema of your login session in ms sql server?
Please explain that what are the basic functions for master, msdb, model, tempdb and resource databases? : SQL Server Architecture
How to use the inserted and deleted pseudo tables?
What is data modification?
How to find index size for each index on table?
What is a mixed extent?
How to declare and use cursor variables?
Define left outer join?
What does set rowcount do?
What is ems sql management studio? : sql server management studio