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 do you understand by the denormalisation?
Explain the categories of stored procedure?
What are the difference between primary key and unique key? : sql server database administration
What is catalog views?
What are the different types of join?
How do I trace a query in sql server?
How does recursive cte works in sql server?
If user is owning any SQL Objects, can we drop that user
What is store procedure?
What do you understand by check constraint in sql server?
whats new about truncate in sql server 2008?
What is the use of attributehierarchyvisible ? : sql server analysis services, ssas
In which files does sql server actually store data?
what is raid? : Sql server database administration
Why does a sql statement work correctly outside of a user-defined function, but incorrectly inside it?