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 is sql server replication? : sql server replication

528


What is sql server profiler trace data file?

575


What do you understand by user-defined function in the sql server?

482


What is the recovery model? List the types of recovery model available in sql server?

479


How fixed length strings are truncated and padded?

540






What are extended events in sql server?

500


Do you know what is a trace frag? Where do we use it?

544


Does the unique constraint create an index?

555


What is model database in sql server?

562


How to update multiple rows with one update statement in ms sql server?

627


Your table has a large character field there are queries that use this field in their search clause what should you do?

497


What is function of ROLLUP ?

640


what are the critical issues you have resloved in your company

1544


what is the sql equivaent of the dataset relation object ?

1561


What are the 10 characteristics of data quality?

518