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 the different SQL Server Versions you have worked on?

561


what are constraints? Explain different types of constraints? : Sql server database administration

507


How can we rewrite sub-queries into simple select statements or with joins?

530


What are two difference between sql azure and azure tables?

139


What xml support does the sql server extend?

533






Why olap is used?

573


What is the difference between having clause and where clause in sql server?

577


Tell about MOM Tool(Microsoft Operator Manager)?

1414


How you can add messages to the nt event log from within a stored procedure?

577


What is create command?

527


Why variables called the most powerful component of ssis?

597


What are different types of join?

765


What is a partition key?

523


Can primary key be null?

517


Where is my database stored on the hard disk in ms sql server?

597