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
What is the difference between row_number and dense_rank?
What is a trigger? Why we need it?
What happens if null values are involved in arithmetic operations?
Explain atomicity?
What is database dimension? : sql server analysis services, ssas
What is indexing explain it with an example?
Is natural join and equi join same?
Explain transaction server distributed transaction?
What languages bi uses to achieve the goal?
1.what is the diff between nolock optimizer and read uncommitted isolation? 2.what is the diff between revoke and deny? 3.what is percieved down time? 4.whether password protection are required for backups?if yes why?if no why? 5.what is fill factor? 6.what is cost analysis? 7.what is mean by piece meal restore? 8.what is 'rowguidcol'? 9.impersonate permission? 10.what is selectivity?
Can a table have 2 primary keys?
Can we use max in where clause?
I have triggers,views,functions,stored Procedures for a table. When I am dropping that table which objects are deleted?
How to provide default values to function parameters?
How do indexes help, types?