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 is the difference between dataadapter and datareader?
what is the primary use of the model database? : Sql server administration
What are the types of processing and explain each? : sql server analysis services, ssas
Can group functions be used in the order by clause in ms sql server?
How to change the data type of an existing column with "alter table" statements in ms sql server?
How many primary keys are possible in a table?
Explain about system stored procedure?
What is tcl in sql server?
What are different types of roles provided by ssrs?
What are the five major components of a dbms?
what changed between the previous version of sql server and the current version? : Sql server database administration
How do I delete a sql server database?
Can you explain various data region available in ssrs with their use?
Tell me about normalization in DBMS.
What is candidate key with example?