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
How to create and drop temp table in sql server?
How to disable a login name in ms sql server?
Mention the differences between local and global temporary tables.
How to create an multi-statement table-valued function?
How to Check if table exists in sql server?
List types of tables in SQL Azure?
Can you explain what are commit and rollback in sql?
What is 2nf in normalization?
What are cascading parameters in ssrs reports?
What methods do you follow to protect from sql injection attack?
What is difference in performance between insert top (n) into table and using top with insert?
Your company has 50 branches all over the country all the branches, including the head office have sql server as the database every night all 50 branches upload certain information to the head office which replication topology is best suited for the above scenario?
What is blocking?
What is the maximum number of index per table?
What is snapshot replication?