微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sqlserver增量更新另一个表的缺失数据

declare @tname varchar(100),@tid int declare cur_table cursor for select [name],[object_id] from sys.objects where [type]='u' open cur_table fetch next from cur_table into @tname,@tid while @@fetch_status=0 begin     declare @sql varchar(500),@pri_key varchar(50)     declare @str varchar(8000),@condition varchar(500)     set @str=''     set @condition=''          select @str=[name]+','+@str from sys.columns where [object_id]=@tid     and [name] not in(select [name] from syscolumns where xtype=189)     and [name] not in(select [name] from sys.columns where is_identity=1)     order by column_id desc     set @str=substring(@str,1,len(@str)-1)              select top 1 @condition=' and '+column_name+' not in (select '+column_name+' from     '+@tname+' where 1=1' from @R_759_4045@ion_schema.key_column_usage     where constraint_name like 'pk%' and table_name=(''+@tname+'')     order by column_name asc     print @condition          declare @c2 varchar(500),@pri_count int     set @c2=''     select @pri_count=isnull(count(*),0) from @R_759_4045@ion_schema.key_column_usage     where constraint_name like 'pk%' and table_name=(''+@tname+'')         if @pri_count>1     begin         select top (@pri_count-1) @c2=' and '+column_name+'=b.'+column_name +@c2         from @R_759_4045@ion_schema.key_column_usage         where constraint_name like 'pk%' and table_name=(''+@tname+'')             order by column_name desc     end     print @c2     set @condition=@condition+@c2+')'             if exists(select 1 from test..sysobjects where [name]=(''+@tname+''))     begin         set @sql='insert into '+@tname+'('+@str+')                   select '+@str+' from test..'+@tname+' b                   where 1=1'+@condition         print @sql         --exec(@sql)     end     fetch next from cur_table into @tname,@tid end close cur_table DEALLOCATE cur_table go

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐