English 中文(简体)
How can I connect to an external database from a sql statement or a stored procedure?
原标题:

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
最佳回答

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

问题回答

Yep -- there s two methods: either use the function OPENROWSET, or use linked servers. OPENROWSET is useful for ad-hoc single statements, but if you re going to be doing this regularly, read up on linked servers as they allow you to do exactly what you ve specified in your SQL Statement ... e.g.,

SELECT database.owner.table for local data
SELECT server.database.owner.table for remote data

And yes, you can mix and match to do joins twixt local and remote. Note though that you ll need to be caureul if you do joins against large tables that exist on the remote server as the query could take a long time to exexute...

Yes, you can. You should take a look at linked servers for starters. You can also use OPENROWSET to hit them directly with no linked server.

Easiest way :

  • Click connect to server
  • when it asks for server name use: 192.168.X.X,1433SQLEXPRESS insted of YOURPCSQLEXPRESS

(The ip and opened port of target sql server)

  • Type correct username and password
  • Done!




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签