SQL Server remote update target table data

Share a stored procedure for remotely updating the target database data, which is suitable for updating the database with the same column name, the primary key is Int type, and can be remotely linked.

 

** Tips: If you need to reprint this article, please indicate the source of the content. **

 

Link to this article: http://www.cnblogs.com/grom/p/9002943.html 

 

USE [Table]--Switch to the source table, which is the table with the latest data
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: <Update remote data in batches, only supports int table with primary key>
-- = =============================================
CREATE PROCEDURE [dbo]. [proc_DataUpdate]
@TargetInstance nvarchar(max),
@TargetDBName nvarchar(max),
@TargetUID nvarchar(max),
@TargetPWD nvarchar(max),
@LocalDBName nvarchar(max),
@PK_ID nvarchar(max),--Primary key column ( must be a number)
@Column nvarchar(max),--Update the set of column names
@ExecSize int--The number of each execution
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;--Open comments can not display the execution process, improve the speed

begin try
  -- take the maximum value
  set @sql = 'select @MaxID=MAX ('+@PK_ID+') from '+@LocalDBName;
  exec sp_executesql @sql,N'@MaxID int out',@MaxID out --loop
  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;

    --Record execution maximum
    SET @SQL='select @NumMax=MAX('+@PK_ID+') from ##tmp_table';
    exec sp_executesql @sql,N'@NumMax int out',@NumMax out;

    --Record execution minimum
    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 code
  Error_severity() as ErrorSeverity, --Error severity level, the level is less than 10 try catch can't catch
  Error_state() as ErrorState , --Error status code
  Error_Procedure() as ErrorProcedure , -- The name of the stored procedure or trigger where the error occurred.
  Error_line() as ErrorLine, -- the line number where the error occurred
  Error_message() as ErrorMessage -- the specific information of the error
  drop table ##tmp_table;

end catch
END

execute stored procedure

USE [table] --源表
GO

DECLARE @return_value int

EXEC @return_value = [dbo].[proc_DataUpdate]
@TargetInstance = N'',--The remote database instance is not in the same domain as the target database, do not use the intranet address
@TargetDBName = N'',--Remote database name
@TargetUID = N'', --username@TargetPWD=N
'',--password
@LocalDBName=N'',--used to update table name (source table)
@PK_ID =N'',--primary key column (required Int)
@Column='ID,Name',--Update column name collection example'A,B,C'
@ExecSize=200--The number of each execution

SELECT 'Return Value' = @return_value

GO

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325669649&siteId=291194637