Join Tables From Different Databases In SQL Server 2008

Here I am going to explain how to join to a table in another database.
  • 1616

Introduction

Here I am going to explain how to join to a table in another database. Join operator is used to fetch data from two or more than two table.

SQL Server can store tables in multiple databases. When you run a SELECT statement against one database, you can join a table in another database if you have appropriate permissions. To join tables from different database, you must prefix the table name in the another database with the name of that database.

For example, let's say you need to join the Student table in the person database with the Student1Details table in the master database. To do that you must specify table name along with their database name. Syntax of a table name that's qualified with a database name is:

Syntax

database_name.table_name

Statement that is used to fetch data from Student1Details table in master database


JoinTable02.jpg


Statement that is used to fetch data from Student table in person database


JoinTable01.jpg

How do I join across database

SELECT Name,S2.Result

FROM master.dbo.Student1Details S1

JOIN person.dbo.Student S2

ON S1.ID=S2.ID

Output:

JoinTable03.jpg

© 2013 dotNetheaven. All rights reserved.