How do you join tables from two different SQL Server instances in one SQL query

Tarik picture Tarik · Sep 14, 2009 · Viewed 112.5k times · Source

Possible Duplicate:
Selecting data from two different servers in SQL Server

How can I join two tables, which are located two different SQL Server instances, in one query?

Answer

Scott Anderson picture Scott Anderson · Sep 14, 2009

The best way I can think of to accomplish this is via sp_addlinkedserver. You need to make sure that whatever account you use to add the link (via sp_addlinkedsrvlogin) has permissions to the table you're joining, but then once the link is established, you can call the server by name, i.e.:

SELECT *
FROM server1table
    INNER JOIN server2.database.dbo.server2table ON .....