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 / mohan
create table #temp(empid int, ename varchar(10),sal int,
deptid int)
insert into #temp values(11, 'Ram', 10000, 10)
insert into #temp values(11, 'Ram',10000,10)
insert into #temp values(22, 'Raj', 20000, 20)
insert into #temp values(22, 'Raj', 20000, 20)
insert into #temp values(33, 'Anil', 15000, 30)
insert into #temp values(33, 'Anil', 15000, 30)
insert into #temp values(44,'bbb',11111,40)
select * from #temp
set rowcount 1
delete from #temp where empid in(select empid from #temp
group by empid having count(*)>1)
while @@rowcount>0
begin
delete from #temp where empid in(select empid from #temp
group by empid having count(*)>1)
end
set rowcount 0
select * from #temp
| Is This Answer Correct ? | 0 Yes | 1 No |
Post New Answer View All Answers
what are the steps you will take, if you are tasked with securing an sql server? : Sql server database administration
What is a DBMS, query, SQL?
what's sql server? : Sql server database administration
How to rebuild indexes with alter index ... Rebuild?
What is standby servers? Explain types of standby servers.
What is meant by referential integrity?
What the different types of Replication and why are they used?
Which joins are sql server default?
Define Business Edition in SQL Azure?
What is the difference between executequery () and executeupdate ()?
what do you understand by change data capture?
Explain sql server service broker?
How do I edit a procedure in sql server?
Difference between report and query parameter. Why do we need different type of parameter?
What is user-defined functions? What are the types of user-defined functions that can be created?