Without details, it is difficult to give direct examples, but here is the basic idea:
First, outside the stored procedure, the host server (the server on which the stored procedure will be turned on) must know about the second server, including (possibly) login information.
On the main server, run the sp_addlinkedserver stored procedure. This needs to be done only once:
exec sp_addlinkedserver @server=’(your second server)‘;
(, , ), sp_addlinkedsrvlogin proc:
exec sp_addlinkedsrvlogin @rmtsrvname=’(your second server)‘,@useself=false, @rmtuser=’yourusername‘, @rmtpassword=’yourpassword‘;
:
SELECT table1.*
FROM table1
INNER JOIN [secondserver].[database].[schema].[table] AS table2 ON
table1.joinfield = table2.joinfield