Delete duplicate rows from a table without primary key by
using a single query
Table Employee
empname salary
A 200
B 300
A 200
C 400
D 500
D 500
Output should be
A 200
B 300
C 400
D 500
Answer Posted / swati tripathi
declare myCursor cursor for
select empid
from employee
group by empid
having count(*) > 1
declare @EmpId int
OPEN myCursor;
FETCH NEXT FROM myCursor INTO @EmpId
WHILE @@FETCH_STATUS = 0
BEGIN
delete top(select count(*)-1 from employee where
empid=@EmpId) from employee where empid=@EmpId
FETCH NEXT FROM myCursor INTO @EmpId
END
close myCursor
deallocate myCursor
| Is This Answer Correct ? | 11 Yes | 0 No |
Post New Answer View All Answers
What is a partitioned view?
Tell me what do you understand by a view? What does the with check option clause for a view do?
What are the advantages of partitioning?
How can I add Reporting Services reports to my application?
What is delete query?
How to drop existing views from a database in ms sql server?
What is proper subset of candidate key?
What is the use of for clause?
How do you drop an index?
What is the template in sql?
Explain the creation and execution of a user-defined function in the sql server?
What are extended events in sql server?
How is table type constraint applied to a table?
Can we add a cpu to sql server?
What is the difference between the export /import functions in sql studio and standalone sql manager? : sql server management studio