I have a table EMP in which the values will be like this
EmpId Ename Sal DeptId
11 Ram 10000 10
11 Ram 10000 10
22 Raj 20000 20
22 Raj 20000 20
33 Anil 15000 30
33 Anil 15000 30
I want to delete only duplicate Rows. After Delete I want
the output like this
EmpId Ename Sal DeptId
11 Ram 10000 10
22 Raj 20000 20
33 Anil 15000 30
Answer Posted / siddharth
DECLARE @int as int
DECLARE @cnt as int
WHILE EXISTS (SELECt top 1 EmpId from Emp group by EmpId having count(EmpId)>1 order by EmpId)
Begin
SELECT top 1 @int=EmpId,@cnt=Count(EmpId) from Emp group by EmpId having count(EmpId)>1 order by EmpId
Delete top (@cnt-1) from Emp where EmpId = @int
End
Select EmpId,Ename,Sal,DeptId from Emp order by EmId
| Is This Answer Correct ? | 4 Yes | 0 No |
Post New Answer View All Answers
What do you mean by stored techniques? How would we use it?
Can we create clustered index on composite key?
What are scalar functions?
What happens if you add a new index to large table?
Explain how would you store your query in an SSRS report or a Database server?
What is 2nf in normalization?
How to add a new column to an existing table with "alter table ... Add" in ms sql server?
What happens when transaction log is full?
How to create user defined functions with parameters?
What are rows and columns?
What is primary key and example?
What is 2nf example?
What is the openxml statement in sql server?
What are the different normalization forms?
Explain the steps needed to create a scheduled job?