Pergunta

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
Foi útil?

Solução

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top