本文给大家分享一个远程更新目标库数据的存储过程,适用于更新列名一致,主键为int类型,可远程链接的数据库。
use [table]--切换到源表,就是数据最新的那个表 go /****** object: storedprocedure [dbo].[proc_dataupdate] script date: 2018/5/4 15:08:56 ******/ set ansi_nulls on go set quoted_identifier on go -- ============================================= -- author: <grom> -- create date: <2018-05-04> -- description: <分批更新远程数据,仅支持主键为int表> -- ============================================= create procedure [dbo].[proc_dataupdate] @targetinstance nvarchar(max), @targetdbname nvarchar(max), @targetuid nvarchar(max), @targetpwd nvarchar(max), @localdbname nvarchar(max), @pk_id nvarchar(max),--主键列(必须为数字) @column nvarchar(max),--更新列名集合 @execsize int--每次执行数量 as declare @sql nvarchar(max), @nummax int=0, @nummin int=0, @maxid int begin -- set nocount on added to prevent extra result sets from -- interfering with select statements. -- set nocount on;--打开注释可不显示执行过程,提高速度 begin try --取最大值 set @sql = 'select @maxid=max('+@pk_id+') from '+@localdbname; exec sp_executesql @sql,n'@maxid int out',@maxid out --循环 while(@nummax<@maxid) begin if exists (select * from tempdb.dbo.sysobjects where id = object_id(n'tempdb..##tmp_table') and type='u') drop table ##tmp_table; set @sql = 'select top '+cast(@execsize as nvarchar(1000))+' '+ @column +' into ##tmp_table from '+@localdbname+' where '+@pk_id+'>'+cast(@nummax as nvarchar(150)); exec sp_executesql @sql; --记录执行最大值 set @sql='select @nummax=max('+@pk_id+') from ##tmp_table'; exec sp_executesql @sql,n'@nummax int out',@nummax out; --记录执行最小值 set @sql='select @nummin=min('+@pk_id+') from ##tmp_table'; exec sp_executesql @sql,n'@nummin int out',@nummin out; set @sql='delete openrowset(''sqloledb'','''+@targetinstance+''';'''+@targetuid+''';'''+@targetpwd+''',['+@targetdbname+'].[dbo].['+@localdbname+']) where '+@pk_id+' between '+cast(@nummin as nvarchar(200))+' and '+cast(@nummax as nvarchar(200)); exec sp_executesql @sql; set @sql='insert into openrowset(''sqloledb'','''+@targetinstance+''';'''+@targetuid+''';'''+@targetpwd+''',['+@targetdbname+'].[dbo].['+@localdbname+']) ('+@column+') select '+ @column +' from ##tmp_table' exec sp_executesql @sql; end --删除多余数据 set @sql='delete openrowset(''sqloledb'','''+@targetinstance+''';'''+@targetuid+''';'''+@targetpwd+''',['+@targetdbname+'].[dbo].['+@localdbname+']) where '+@pk_id+' >'+cast(@nummax as nvarchar(200)); drop table ##tmp_table; print 'success'; end try begin catch select error_number() as errornumber, --错误代码 error_severity() as errorseverity, --错误严重级别,级别小于10 try catch 捕获不到 error_state() as errorstate , --错误状态码 error_procedure() as errorprocedure , --出现错误的存储过程或触发器的名称。 error_line() as errorline, --发生错误的行号 error_message() as errormessage --错误的具体信息 drop table ##tmp_table; end catch end
执行存储过程
use [table] --源表 go declare @return_value int exec @return_value = [dbo].[proc_dataupdate] @targetinstance = n'',--远程数据库实例 如目标库不在一个域,切勿使用内网地址 @targetdbname = n'',--远程数据库名称 @targetuid = n'',--用户名 @targetpwd = n'',--密码 @localdbname=n'',--用于更新表名 (源表) @pk_id =n'',--主键列(必须为int) @column='id,name',--更新列名集合 例 'a,b,c' @execsize=200--每次执行条数 select 'return value' = @return_value go
总结
以上所述是www.887551.com给大家介绍的sql server 远程更新目标表数据的存储过程,希望对大家有所帮助