How can I connect to an external database from a sql statement or a stored procedure?

Matt picture Matt · Dec 21, 2009 · Viewed 41.9k times · Source

When running a SQL statement or a stored procedure on a database, can you connect to an external database and pull data from there?

something like:

SELECT a.UserID, b.DataIWantToGet 
  FROM mydb.Users as a, externaldb.Data as b

Answer

OMG Ponies picture OMG Ponies · Dec 21, 2009

You'll need to setup a Linked Server instance. Then you can reference the external database as though it were a SQL Server database.