-1

Can I use the below query to join 2 tables of different databases of same server. Also let me know in which database I have to execute this query.

SELECT ...
FROM A.table t1
  JOIN B.table2 t2 ON t2.column = t1.col
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Shubham Agarwal
  • 13
  • 1
  • 1
  • 2

1 Answers1

10

You just need to change that query a bit

Select * from Database1.[dbo].Table1 tab1
join Database2.[dbo].Table2 tab2 on tab1.ID = tab2.ID

or

Select * from Database1..Table1 tab1
join Database2..Table2 tab2 on tab1.ID = tab2.ID

Change names of databases, tables and IDs and give it a go

You can use it in all of connections on your server (every database), as your query have reference to databases

Veljko89
  • 1,813
  • 3
  • 28
  • 43
  • Thanks for your prompt responce Veljko89... But mail question is that in which DB i have to execute the query.. – Shubham Agarwal Oct 23 '15 at 11:16
  • You can use it in all of connections on your server (every database), as your query have reference to both databases – Veljko89 Oct 23 '15 at 11:18