Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

how to delete duplicate rows from table in sql server

Answer Posted / victor

this can help if you want to keep only different records

create table test
(
id int,
name varchar(20)
)

insert into test VALUES(1,'test')
insert into test VALUES(2,'test')
insert into test VALUES(2,'test')
insert into test VALUES(3,'test')
insert into test VALUES(4,'test')
insert into test VALUES(5,'test')
insert into test VALUES(6,'test')
insert into test VALUES(7,'test')
insert into test VALUES(7,'test')
insert into test VALUES(7,'test')



select * from test order by 1

while @@rowcount != 0
begin
delete top (1) test where id in
(
select id
FROM test
GROUP BY id having count(id)>1
)
end

select * from test order by 1


-- At the end you will have only differents IDS

Is This Answer Correct ?    9 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do you know if sql server is running on your local system?

1044


Explain subquery and state its properties?

1049


What is named query? : sql server analysis services, ssas

968


What is report snapshot in ssrs?

270


Do you know what are the ways available in sql server to execute sql statements?

1037


Is it true that rules do not apply to data already existing in a database at the time the rule is created?

1044


why would you use sql agent? : Sql server database administration

1218


Explain Capacity planning to create a database? and how to monitor it?

4205


What is a non clustered primary key?

927


What is the difference between Stored Procedures and triggers?

1083


What stored by the model?

1064


On a windows server 2003 active – passive failover cluster, how do you find the node which is active?

1209


What is Sqlpaging in SqlServer 2005 ?

1072


Which trace flags are enabled in sql server?

1022


Can we use having clause without group by?

983