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 the difference between upgrade and migration in sql server?
What is replication with database mirroring? : sql server database administration
What is an index in a database?
Do you know what is difference between stored procedure and user defined function?
What gets stored inside msdb database?
How to set the current database in ms sql server?
You accidentally delete the msdb database what effect does this have on your existing sql databases, and how do you recover?
What does this statement do @@rowcount?
What is row_number function?
You want to check the syntax of a complicated update sql statement without executing it. What command should you use?
How can you control the amount of free space in your index pages?
what are isolation levels? : Sql server database administration
What is user-defined multi-statement table-valued function?
what's new in sql server 2016?
What is the new security features added in sql server 2016? : sql server security