0

How would I join tables from different databases?

example join table_1(db1) and table_2 (db2) and then return some FK values

  • Does SQLS support cross database FK relationships?

  • How would you ensure integrity?

  • How would the syntax look when joining db2 with a FK reference?

SELECT
tb1.ID,
b.Name as Bid,
o.Name as Outcome

From table1 as tb1 
left join bid as b on b.ID = g.ID 
left join outcome as o on o.ID = g.ID
Dallas
  • 35
  • 1
  • 9

1 Answers1

2

The full name of the table on SQL Server has a format

[database].[schema].[table]

So if you have two tables in different databases on the one server you can join them this way.

select * 
  from first_db.dbo.table1 a
  inner join second_database.dbo.table2 b on a.id = b.id

Let me know if you need more explanations.

Alexey Usharovski
  • 1,404
  • 13
  • 31