USE LocalDatabaseName;
GO
--创建链接服务器
IF NOT EXISTS (select * from sys.servers where name = 'RemoteServer')
BEGIN
EXEC sp_addlinkedserver 'RemoteServer', '','SQLNCLI', 'xx.xx.xx.xx'
END
go
--登录链接服务器
EXEC sp_addlinkedsrvlogin 'RemoteServer', 'false', null, 'RemoteServer username', 'RemoteServer password'
go
--将本地表最新数据同步到链接服务器对应表
INSERT INTO OPENQUERY([RemoteServer], 'SELECT * FROM RemoteDatabaseName.dbo.RemoteTableName')
SELECT r.* FROM LocalTableName r
WHERE r.id > (
SELECT maxID FROM OPENQUERY([RemoteServer], 'SELECT MAX(id) maxID FROM RemoteDatabaseName.dbo.RemoteTableName')
)
go
--退出链接服务器
EXEC sp_droplinkedsrvlogin 'RemoteServer', null
go
--删除链接服务器
EXEC sp_dropserver 'RemoteServer', 'droplogins'
Go
参考链接1:https://www.cnblogs.com/tmftmb/p/11607676.html
参考微软官方链接:https://docs.microsoft.com/zh-cn/sql/relational-databases/system-stored-procedures/sp-addlinkedserver-transact-sql?redirectedfrom=MSDN&view=sql-server-2017