精妙的SQL和SQL SERVER 与ACCESS、EXCEL的数据导入导出转换

2020-07-06 05:52:07易采站长站整理

      3、作一个包含所有不重复记录的表:
      select * into temp2 from [表名] where 标志字段id in(select 标志字段id from temp1)
      4、删除重复表:
      delete [表名]
      5、恢复表:
      insert [表名] select * from temp2
      6、删除临时表:
      drop table temp1
      drop table temp2
      ================================
      B:
      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
相关文章 大家在看