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

Is it possible to update the views? If yes, how, if not, why?

545


How to find the service pack installed? : sql server database administration

554


How to convert character strings into numeric values?

579


Is it true, that there is no difference between a rule and a check constraint?

526


In how many ways you can invoke ssrs reports?

103






What is difference between rownum and rowid?

511


Can we join two tables without primary key?

619


How to Update from select query in sql server?

550


What are the advantages of using third-party tools?

556


What is buffer cash and log cache in sql server?

593


Explain the stored procedure?

640


What is a join in sql?

590


How to make a column nullable?

603


What is cursors?

644


Difference between uniqe index and uniqe constraint?

550