SQL SERVER 本地同步数据到远程数据服务器
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
亲力亲为,温故知新!