adspace
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
If you're given a raw data table, how would perform etl (extract, transform, load) with sql to obtain the data in a desired format?
Explain “row_number()” in sql server with an example?
Disadvantages of the indexes?
How to remove duplicate rows from table except one?
If any stored procedure is encrypted, then can we see its definition in activity monitor?
Why we use the openxml clause?
What is a coalesce function?
How to rebuild the master database?
Can one drop a column from a table?
When should you use an instead of trigger?
How do I find the sql server version?
What is clustered index
You have a stored procedure, which execute a lengthy batch job. This stored procedure is called from a trigger you do not want to slow the data entry process you do not want trigger to wait for this batch job to finish before it completes itself what you can do to speed up the process?
Why and when do stored procedure recompile?
How to provide default values to function parameters?