SQL SERVER local synchronization data to a remote data server

The USE LocalDatabaseName; 

the GO 

- Create link server 

the IF  the NOT  EXISTS ( SELECT  *  from and sys.servers WHERE name =  ' the RemoteServer ' ) 

 the BEGIN 

   EXEC the sp_addlinkedserver ' the RemoteServer ' , '' , ' SQLNCLI ' , ' xx.xx.xx.xx ' 

 the END 

Go 

- Log linked server 

EXEC sp_addlinkedsrvlogin ' RemoteServer ' , ' false' , Null , ' the RemoteServer username ' , ' the RemoteServer password ' 

Go 

- the local tables to the latest data synchronization server correspondence table linking 

the INSERT  the INTO  the OPENQUERY ( [ the RemoteServer ] , ' the SELECT * the FROM RemoteDatabaseName.dbo.RemoteTableName ' ) 

the SELECT R & lt. *  the FROM LocalTableName R & lt 

the WHERE r.id > ( 

    the SELECT maxID the FROM  the OPENQUERY ( [ the RemoteServer ] , 'MAX the SELECT (the above mentioned id) maxID the FROM RemoteDatabaseName.dbo.RemoteTableName ' ) 

) 

Go 

- Exit linked server 

EXEC sp_droplinkedsrvlogin ' RemoteServer ' , null 

Go 

- delete a linked server 

EXEC sp_dropserver ' RemoteServer ' , ' droplogins ' 

Go

 

Reference Links 1: https: //www.cnblogs.com/tmftmb/p/11607676.html

Refer to the Microsoft official link: https: //docs.microsoft.com/zh-cn/sql/relational-databases/system-stored-procedures/sp-addlinkedserver-transact-sql redirectedfrom = MSDN & view = sql-server-2017?

Guess you like

Origin www.cnblogs.com/tmftmb/p/11607699.html