一题多解教你SQL语句删除重复记录
2008-10-28 16:13
387 查看
此文章用了四种方法教你如何用SQL语句删除重复记录。
问题:怎样把具有相同字段的纪录删除,只留下一条。
例如:表test里有id,name字段,如果有name相同的记录只留下一条,其余的删除。name的内容不定,相同的记录数不定。
方案1:
1、将重复的记录记入temp1表:
2、将不重复的记录记入temp1表:
3、作一个包含所有不重复记录的表:
4、删除重复表:delete [表名]
5、恢复表:
6、删除临时表:
方案2:
注:set rowcount @max - 1 表示当前缓冲区只容纳@max-1条记录﹐如果有十条重复的﹐就刪除
10条,一定会留一条的。也可以写成delete from 表名。
方案3:
方案4:
可以用IGNORE_DUP_KEY:
注:这里delete原表,再加入不重复的值。也可以通过join只delete原表中重复的值。(T006)
http://tech.ccidnet.com/art/1106/20070425/1069837_2.html
问题:怎样把具有相同字段的纪录删除,只留下一条。
例如:表test里有id,name字段,如果有name相同的记录只留下一条,其余的删除。name的内容不定,相同的记录数不定。
方案1:
1、将重复的记录记入temp1表:
select [标志字段id],count(*) into temp1 from [表名] group by [标志字段id] having count(*)>1 |
insert temp1 select [标志字段id],count(*) from [表名] group by [标志字段id] having count(*)=1 |
select * into temp2 from [表名] where 标志字段id in(select 标志字段id from temp1) |
5、恢复表:
insert [表名] select * from temp2 |
drop table temp1 drop table temp2 |
declare @max integer,@id integer declare cur_rows cursor local for select id,count(*) from 表名 group by id having count(*) > 1 open cur_rows fetch cur_rows into @id,@max while @@fetch_status=0 begin select @max = @max -1 set rowcount @max delete from 表名 where id = @id fetch cur_rows into @id,@max end close cur_rows set rowcount 0 |
10条,一定会留一条的。也可以写成delete from 表名。
方案3:
create table a_dist(id int,name varchar(20)) insert into a_dist values(1,'abc') insert into a_dist values(1,'abc') insert into a_dist values(1,'abc') insert into a_dist values(1,'abc') exec up_distinct 'a_dist','id' select * from a_dist create procedure up_distinct(@t_name varchar(30) ,@f_key varchar(30)) --f_key表示是分组字段﹐即主键字段 as begin declare @max integer,@id varchar(30) , @sql varchar(7999) ,@type integer select @sql = 'declare cur_rows cursor for select '+@f_key+' ,count(*) from ' +@t_name +' group by ' +@f_key +' having count(*) > 1' exec(@sql) open cur_rows fetch cur_rows into @id,@max while @@fetch_status=0 begin select @max = @max -1 set rowcount @max select @type = xtype from syscolumns where id=object_id(@t_name) and name=@f_key if @type=56 select @sql = 'delete from '+@t_name+' where ' + @f_key+' = '+ @id if @type=167 select @sql = 'delete from '+@t_name+' where ' + @f_key+' = '+''''+ @id +'''' exec(@sql) fetch cur_rows into @id,@max end close cur_rows deallocate cur_rows set rowcount 0 end select * from systypes select * from syscolumns where id = object_id('a_dist') |
可以用IGNORE_DUP_KEY:
create table dup (id int identity not null, name varchar(50)not null) go insert into dup(name) values ('abc') insert into dup(name) values ('abc') insert into dup(name) values ('abc') insert into dup(name) values ('abc') insert into dup(name) values ('abc') insert into dup(name) values ('abc') insert into dup(name) values ('abc') insert into dup(name) values ('cdefg') insert into dup(name) values ('xyz') insert into dup(name) values ('xyz') go select * from dup go create table tempdb..wk(id int not null, name varchar(50)not null) go create unique index idx_remove_dup on tempdb..wk(name) with IGNORE_DUP_KEY go INSERT INTO tempdb..wk (id, name) select id, name from dup go select * from tempdb..wk go delete from dup go set identity_insert dup on INSERT INTO dup (id, name) select id, name from tempdb..wk go set identity_insert dup off go select * from dup go |
http://tech.ccidnet.com/art/1106/20070425/1069837_2.html
相关文章推荐
- mysql删除重复记录语句的方法 作者: 字体:[增加 减小] 类型:转载 时间:2010-06-21 我要评论 查询及删除重复记录的SQL语句,虽然有点乱,但内容还是不错的。 . .
- sql语句删除重复的记录的方法
- 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢?
- Oracle 查询并删除重复记录的SQL语句
- 可以删除重复记录的SQL语句
- 找出数据库中重复记录的SQL语句并删除
- 使用SQL语句查询表中重复记录并删除
- 删除数据库中重复记录的SQL语句
- 查询并删除重复记录的SQL语句
- sql语句删除重复的记录的方法
- 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢?
- 存在着些相同的记录,如何能用SQL语句,删除掉重复的呢?
- 使用一条SQL语句删除表中重复记录
- 查询及删除重复记录的SQL语句
- [导入]通过SQL语句删除重复记录
- 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复
- SQL语句快速删除重复记录
- 查询及删除重复记录的SQL语句
- Oracle 查询并删除重复记录的SQL语句
- 使用SQL语句对重复记录查询、统计重复次数、删除重复数据