Can we use join for two different database tables?

Can I use a join operation for two tables from different databases? If so, how to do it?

Both databases are on the same server, and the DBMS is the same.

+49
sql database
Jul 11 2018-12-12T00:
source share
2 answers

SQL Server allows you to combine tables from different databases if these databases are on the same server. The syntax of the connection is the same; the only difference is that you must specify the full table names.

Suppose you have two databases on the same server - Db1 and Db2 . Db1 has a table named Clients with a ClientId column and Db2 has a table called Messages with a ClientId column (let's not understand why these tables are in different databases).

Now, to join the above tables, you will use this query:

 select * from Db1.dbo.Clients c join Db2.dbo.Messages m on c.ClientId = m.ClientId 
+103
Jul 11 2018-12-12T00:
source share
  SELECT ... FROM A.table t1 JOIN B.table2 t2 ON t2.column = t1.col 
0
Jul 11 2018-12-12T00:
source share



All Articles