kaliliP
kaliliP

Reputation: 37

Join 2 tables from different DB

How can I join 2 tables from different DB with SQL Server?

Upvotes: 1

Views: 100

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 175964

Simply just use multipart identifier names if they are in the same server.

SELECT *
FROM [database_1].[dbo].[table_1] t1
JOIN [database_2].[dbo].[table_2] t2
  ON t1.id = t2.id

If not you can use linked servers and 4-part names.

SELECT *
FROM [database_1].[dbo].[table_1] t1
JOIN [linked_server].[database_2].[dbo].[table_2] t2
  ON t1.id = t2.id

Upvotes: 1

Related Questions