Share a remote update target library data storage process

  In this paper to share a remote update target library data storage process for updating the same column names, primary keys for the Int type, remotely linked databases.
USE [Table] - Switch to the source table, that table is the latest data
GO
/ ****** Object:. StoredProcedure [dbo] [proc_DataUpdate] Script a Date: 2018/5/4 15:08:56 ** **** /
the SET the ANSI_NULLS the ON
the GO
the SET the QUOTED_IDENTIFIER the ON
the GO (http://www.0831jlyy.com)
- ======================== =====================
- the Author: <Grom>
- the Create DATE: <2018-05-04>
- the Description: <batch update remote data supports only the primary key for the table int>
- ======================================== =====
the CREATE PROCEDURE [the dbo]. [proc_DataUpdate]
@TargetInstance nvarchar (max),
@TargetDBName nvarchar (max),
@TargetUID nvarchar (max),
@TargetPWD nvarchar (max),
@LocalDBName nvarchar (max),
@PK_ID nvarchar (max), - the primary key columns (Must be a number)
the @Column nvarchar (max), - updating the set of the column name
@ExecSize int-- each time the number of
the AS
DECLARE @sql nvarchar (max),
@NumMax int 0 =,
@NumMin int = 0,
@MaxID int
the BEGIN (http://jlyy0831.com)
- added to the SET Prevent the NOCOUNT the ON Extra Result sets from
- interfering period with the SELECT statements.
- the NOCOUNT the ON the SET; - open Notes may not display process performed, increasing the speed
the begin the try
  - maximum value
  SET @sql = '@ MaxID SELECT MAX = (' + @ + PK_ID ') from' + @ LocalDBName;
  Exec the sp_executesql @ SQL, N '@ MaxID int OUT' , MaxID OUT @
  - cycle
  the while (@NumMax <@MaxID)
  the begin
    IF EXISTS (SELECT * WHERE ID = object_id from tempdb.dbo.sysobjects (N'tempdb tmp_table .. ## ') and type =' the 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(http://www.0834xcjl.com)
begin catch
  select Error_number () as ErrorNumber, - error code
  Error_severity () as ErrorSeverity, - error severity, level not less than 10 try catch capture
  Error_state () as ErrorState, - error status code
  Error_Procedure () as ErrorProcedure, - wrong name of the stored procedure or trigger occurs.
  Error_line () as ErrorLine, - the line number where the error occurred
  Error_message () as ErrorMessage - error specific information
  drop tmp_table Table ##;
End the catch
the END
executes a stored procedure
USE [table] - the source table
the GO
the DECLARE @return_value int
EXEC . @return_value = [the dbo] [proc_DataUpdate]
@TargetInstance = N '', - a remote database instance as the target library is not a domain, do not use the network address
@TargetDBName = N '', - the name of the remote database
@TargetUID = N '', - user name
@TargetPWD = N '', - password
@ LocalDBName = N '', - for updating the table (source table)
@PK_ID = N '',
@ Column = 'ID, Name' , - updating the set of the column name in Example 'A, B, C'
@ ExecSize = 200-- each execution number of
the SELECT 'the Return the Value' = @return_value
the GO
above is small to give introduce SQL Server remotely update the target table the data storage process, we want to help.

Guess you like

Origin www.cnblogs.com/HanaKana/p/12005169.html