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


Please Help Members By Posting Answers For Below Questions

What are statistics?

778


Explain what is lock escalation and what is its purpose?

684


what are different types of backups available in sql server? Given a particular scenario, how would you go about choosing a backup plan? : Sql server database administration

685


What is the difference between join and inner join?

723


What is create statement?

680






Explain the functionalities that views support?

856


Explain the working of sql privileges?

800


what purpose does the model database serve? : Sql server database administration

670


When does the auto update index statistics feature in sql server turn itself on?q) what specific conditions database should meet, before you can bulk copy data into it using bcp?

756


Why the trigger fires multiple times in single login?

882


How to disable stored procedure sql server?

1220


What is raid, and how it can influence database performance?

762


Is the primary key column of a table an index in ms sql server?

701


How to disable a login name in ms sql server?

750


How to create a simple stored procedure in ms sql server?

704