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 are the different SQL Server Versions you have worked on?
what are constraints? Explain different types of constraints? : Sql server database administration
How can we rewrite sub-queries into simple select statements or with joins?
What are two difference between sql azure and azure tables?
What xml support does the sql server extend?
Why olap is used?
What is the difference between having clause and where clause in sql server?
Tell about MOM Tool(Microsoft Operator Manager)?
How you can add messages to the nt event log from within a stored procedure?
What is create command?
Why variables called the most powerful component of ssis?
What are different types of join?
What is a partition key?
Can primary key be null?
Where is my database stored on the hard disk in ms sql server?