Thursday, July 16, 2009

MS SQL: Join tables of different database

Assume you have two database, Db1 and Db2.

Employee table is in Db1 and Department in Db2

Query:

select emp_name, dept_name from Db1.dbo.Employee a, Db2.dbo.Department b where a.dept_id = b.dept_id

Syntax is Database name followed by .dbo. followed by table name

eg. Db1.dbo.Employee

Similarly you can use above syntax for any type of query.