数据库开发
网络新概念,云计算、大数据、O2O、电商。。。。
网络新概念,云计算、大数据、O2O、电商。。。。
2019-02-26 11:14:50
有时候数据产生的重复项目,所有的列,下面是如果找出有重复数据的表并处理
declare @tblname varchar(200) declare @object_id int declare tbl_cursor CURSOR for select name,object_id from sys.all_objects where type='u' open tbl_cursor FETCH NEXT FROM tbl_cursor INTO @tblname,@object_id while @@FETCH_STATUS = 0 Begin declare @sql varchar(8000) declare @str varchar(8000) declare @selectstr varchar(8000) set @selectstr='' set @str='' --print @tblname,@object_id declare @colname varchar(200),@coltypeid int declare col_cursor CURSOR for select name,system_type_id from sys.columns where object_id=@object_id order by column_id open col_cursor FETCH NEXT FROM col_cursor INTO @colname,@coltypeid WHILE @@FETCH_STATUS = 0 BEGIN if @coltypeid not in (189) set @str=@str+'['+@colname+'],' if @coltypeid not in (34,35,99,189) set @selectstr=@selectstr+'['+@colname+'],' FETCH NEXT FROM col_cursor INTO @colname,@coltypeid END close col_cursor deallocate col_cursor set @str=substring(@str,0,len(@str)) set @selectstr=substring(@selectstr,0,len(@selectstr)) print ' if (select count(*) from '+@tblname+')!=(select count(*) from (select distinct '+@selectstr+' from '+@tblname+') as a) begin ' if exists(select * from sys.columns where OBJECT_ID=@object_id and is_identity=1) print ' print '' SET IDENTITY_INSERT '+@tblname+' ON''' print ' print '' select distinct '+@str+' into #tmpt from '+@tblname+' truncate table '+@tblname+' insert into '+@tblname+'('+@str+') ''' print ' print '' select '+@str+' from #tmpt drop table #tmpt ''' if exists(select * from sys.columns where OBJECT_ID=@object_id and is_identity=1) print ' print ''SET IDENTITY_INSERT '+@tblname+' OFF''' print ' print ''go'' ' print ' end ' fetch next from tbl_cursor into @tblname,@object_id End close tbl_cursor deallocate tbl_cursor
上一篇:金蝶k3库存账龄分析表存储过程