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

Do you know what is replace and stuff function in sql server?

469


What is the difference between ddl,dml and dcl commands?

612


Define right outer join?

545


What is the difference between createstatement and preparedstatement?

494


Describe in brief sql server monitoring ways.

589






What are the tables in sql?

623


What are a database and a data warehouse?

566


Is sql server difficult to learn?

520


What is a domain constraint give an example?

563


What is a performance monitor?

554


Can you give me some DBCC command options?(Database consistency check) - DBCC CHECKDB - Ensures that tables in the db and the indexes are correctly linked.and DBCC CHECKALLOC - To check that all pages in a db are correctly allocated. DBCC SQLPERF - It gives report on current usage of transaction log in percentage. DBCC CHECKFILEGROUP - Checks all tables file group for any damage.

2028


Explain multiserver query

555


What is openxml in sql server?

616


what is package and it uses and how can u call a package

1559


Explain transaction server explicit transaction?

505