how to select data from another sql server server tables in sql script?
I want to know how to query data from another sql server instances' tables
in sql script.
I am writing sql script running on 127.0.0.1\SQLINSTANCE1 but inside the
script there is going to select data from 127.0.0.2\SQLINSTANCE2 then
return the result data as part of calculation.
After googled, there seems uses the sp_addlinkedserver and
sp_addlinkedsrvlogin stored procedures for doing that, but unfortunately
there is no complete sample working.
i have some work here but not working as expected. For example, when it
was executed once more then there would pop up some error like the server
already linked. but how i can execute this script for linking the server
and then abandoning it.
DECLARE @remoteserver VARCHAR = '127.0.0.2\SQLINSTANCE2';
EXEC master.sys.sp_addlinkedserver @server = @remoteserver
, @srvproduct = 'SQL Server';
EXEC master.sys.sp_addlinkedsrvlogin @rmtsrvname = @remoteserver
, @useself = 'false'
, @locallogin = NULL
, @rmtuser = 'sa'
, @rmtpassword = 'password';
To the other side, how can i create an alias for this ugly remoteserver
name? I rather to use some elegant name like RS in select * from
[RS].[db].[dbo].[table]
Really grateful if i can get some help here!
Thanks a lot!!!
No comments:
Post a Comment