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


Please Help Members By Posting Answers For Below Questions

What is difference between global temporary tables and local temporary tables?

658


What is GUID in sql server?

637


Does union all remove duplicates?

607


What are the advantages of using stored procedures?

536


What are the advantages of using stored procedures in sql server?

506






How use inner join in sql server?

613


What is an expensive query?

536


What are secondary xml indexes?

562


What is data source object?

547


How to download microsoft sql server 2005 express edition?

574


Why I am getting this error when dropping a database in ms sql server?

588


How to create a view and a stored procedure in ms sql server using "create view/procedure" statements?

509


Tell me extended events in sql server 2008?

557


What are the main differences between #temp tables and @table variables and which one is preferred?

547


How to list all user names in a database?

590