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
whats the maximum size of view state??
Explain the dirty pages?
what is a sub-report?
Which table keeps the locking information?
Explain user defined functions?
What is the difference between varchar and nvarchar datatypes?
How adventureworkslt tables are related?
How to declare a cursor with "declare ... Cursor" in ms sql server?
What is coalesce in sql server?
How does recursive cte works in sql server?
What are the properties of the relational tables?
What are types of storage modes? : sql server analysis services, ssas
what is the main function of a query parameter?
What are the elements of dbms?
Write a SQL query to make a column as unique?