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
Explain an automatic checkpoint
Once setting replication, can you have distributor on sql server 2005, publisher of sql server 2008?
What is the report model project?
What happens if time-only values are provided as date and time literals?
how many clustered indexes can be created on a table? : Sql server database administration
Write SQL queries on Self Join and Inner Join.
can any body tell me how to know the password of current user in sql server
What are the 10 characteristics of data quality?
Difference between group by clause and having clause in SQL?
Why do we backup Active Directory ?
Does group by or order by come first?
How to fine-tune reports?
What are the basic features of a trigger in ms sql server?
What is difference between after and before?
What are the rules to use the rowguidcol property to define a globally unique identifier column?