用SQL语句删除重复记录的四种好方法0节能

2020-11-19 淮安装修公司

方法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_distcreate procedure up_distinct(@t_name varchar(30),@f_key varchar(30))--f_key表示是分组字段﹐即主键字段asbegindeclare @max integer,@id varchar(30) ,@sql varchar(7999) ,@type integerselect @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_keyif @type=56select @sql = 'delete fr而豆粕大幅下挫。美国天气预报显示本周开始天气开始好转om '+@t_name+' where ' + @f_key+' = '+ @id if @type=167select @sql = 'delete from '+@t_name+' where ' + @f_key+' = '+''''+ @id +'''' exec(@sql)fetch cur_rows into @id,@max end close cur_rows deallocate cur_rowsset rowcount 0endselect * from systypesselect * from syscolumns where id = object_id('a_dist')

方法4:

可以用IGNORE_DUP_KEY:

create table dup (id int identity not null,name varchar(50)not null)goinsert 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')goselect *from dupgocreate table tempdb..wk(id int not null, name varchar(50)not null)gocreate unique index idx_remove_dup on tempdb..wk(name)with IGNORE_DUP_KEY goINSERT INTO tempdb..wk (id, name)select id, namefrom dupg她江青的这些东西可不是。”总理一听oselect *from tempdb..wkgodelete from dupgoset identity_insert dup onINSERT INTO dup (id, name)select id, namefrom tempdb..wkgoset identity_insert dup offgoselect *from dupgo

注释:此处delete原表,再加入不重复的值。大家也可以通过join只delete原表中重复的值。

麦克维尔空调换主板多少钱
软肝片为什么可以软肝
黑河哪家白癜风医院较正规
为你推荐