因项目中经常需要从其他远程服务器上读取数据,故需要编写SQL远程读取SQL,记录,以备后用。--by RYHAN
1 DECLARE @SQLString varchar(2000) 2 DECLARE @SourceDataBaseName nvarchar(30) 3 DECLARE @RemoteIP nvarchar(30) 4 DECLARE @RemoteAccount nvarchar(30) 5 DECLARE @RemotePwd nvarchar(30) 6 DECLARE @DBOwner nvarchar(30) --数据库所有者 7 8 9 SET @SourceDataBaseName ='TestDB' 10 SET @RemoteIP ='192.168.1.110' 11 SET @RemoteAccount ='sa' 12 SET @RemotePwd ='sasa' 13 SET @DBOwner ='dbo' 14 15 16 17 --链接到远程数据库 18 EXEC sp_addlinkedserver @server='RemoteDBServer',@provider='sqloledb',@srvproduct='',@datasrc=@RemoteIP 19 --EXEC sp_addlinkedsrvlogin @rmtsrvname='RemoteDBServer',@useself='false',@locallogin=@RemoteAccount,@rmtuser=@RemoteAccount,@rmtpassword=@RemotePwd 20 EXEC sp_addlinkedsrvlogin 'RemoteDBServer','false',NULL,@RemoteAccount,@RemotePwd 21 22 23 24 --DO SQL 25 SET @SQLString =' SELECT * FROM RemoteDBServer.['+ @SourceDataBaseName +'].['+@DBOwner+'].[TestTable] NOLOCK ' 26 27 PRINT @SQLString 28 EXEC(@SQLString) 29 30 31 32 --从远程数据库退出登录并关闭链接 33 EXEC sp_droplinkedsrvlogin 'RemoteDBServer' , NULL 34 EXEC sp_dropserver 'RemoteDBServer'
因项目中经常需要从其他远程服务器上读取数据,故需要编写SQL远程读取SQL,记录,以备后用。--by RYHAN