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

explain what is raid and what are different types of raid configurations? : Sql server database administration

727


What are the different index configurations a table can have?

692


Name some of the open source software that you can use in alternative to SSR?

134


What is database architecture? : SQL Server Architecture

737


What is 'Join' and explain its various types.

802






What are the key configuration files for sql server reporting services ?

150


What are the recovery models in sql server 2000?

708


What are Row versions of DataRow?

784


Is it possible for a stored procedure to call itself or recursive stored procedure? How many levels of sp nesting are possible?

665


What is policy management?

735


How to get @@error and @@rowcount at the same time?

767


Explain about builtinadministrator?

768


Would you store your query in a ssrs report or a database server? State the reason why?

132


How to find the second highest salary of an employee?

774


Where are SQL server users names and passwords are stored in sql server?

901