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
What are cursors? Explain different types of cursors. What are the disadvantages of cursors? How can you avoid cursors?
What is a DBMS, query, SQL?
Where are full-text indexes stored?
What does <> symbol mean?
What are acid properties of transaction?
What is the use of sql profiler in sql server 2012?
Tell me the phases a transaction has to undergo?
What are the steps you must follow to hide sql server instances?
Define msdb database?
please differentiate between delete and truncate?
What is the datatype of rowid?
How to access the deleted record of an event?
How can we migrate from SQL server to SQL Azure?
What is difference between createstatement and preparedstatement?
How to choose all records from the table?