Danilo
Danilo

Reputation: 2036

Join 2 tables from different databases

Is it possible joining two tables from different databases in php using MSSQL server and ADODB connections?

/*Connect DB1*/
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn->open("PROVIDER=SQLOLEDB;SERVER=SRV;UID=user;PWD=pwd;DATABASE=DB1;");

/*Connect DB1*/
$conn2 = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn2->open("PROVIDER=SQLOLEDB;SERVER=SRV;UID=user;PWD=pwd;DATABASE=DB2;");

I need to perform a JOIN query on these two databases, something like this:

select col from DB1.table1 JOIN DB2.table

Upvotes: 1

Views: 919

Answers (1)

Wagner Leonardi
Wagner Leonardi

Reputation: 4446

you don't need 2 connections to do that, you can do this just with one connection since connection user has access to both databases

You just need to put database name before table name

[database_name].[dbo].[table_name]

Here is how you could do that from your given example:

SELECT a.col FROM [DB1].[dbo].[table1] a
INNER JOIN [DB2].[dbo].[table] b ON a.col = b.col

Upvotes: 2

Related Questions