Popular Posts

Wednesday, February 2, 2011

Connecting Two Remote Databases in a single SQL Query





Background

Every Microsoft SQL Server database, there is a folder called Server Objects. Within that folder there is a folder called "Linked Servers". If you right click on that folder and hit New Linked Server, it will open up a new linked server wizard.

New Linked Server Wizard

By following the steps there, you can setup a new linked server.

First, type in the name or IP address of the server where it says "Linked server". For example: "1.0.9.46".

For a remote SQL database choose SQL Server as the server type. Then select the security page from the left, and enter the login credentials under the "Be made using this security context" option: Enter the username where it says remote login, and the password below where it says "With password."


Creating a Query using the Linked server.

Once your linked server is setup, you can execute a query like the following:

SELECT *
FROM
[1.0.9.46].DatabaseName.dbo.tableName t
JOIN users ON user.ID = t.userID;

Enjoy!!!

No comments: