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
Explain tablesample?
How to optimize stored procedures in sql server?
How retrieve field names from the table in SQL through JAVA code?
What is usually the first word in a sql query?
Which trace flags are enabled in sql server?
Can we join two tables without primary key?
What are audit control procedures?
Can we call future method from trigger?
What are the different editions available in sql server 2000?
Can the query output be sorted by multiple columns in ms sql server?
What is a constant or literal in ms sql server?
Tell me in brief how sql server enhances scalability of the database system?
What is a mixed extent?
How to set the current database in ms sql server?
How to drop an existing stored procedure in ms sql server?