SQL Server > Query

Joining tables form two different databases in SQL Server

How to write query to join tables form two different databases in SQL Server?

To join tables from two different databases, we can use fully qualified names of the tables in this format [databasename].[owner].[tablename].

SELECT ac.AutoId, ac.FullName, pd.Age
FROM [IHaveBeenRenamed].[dbo].Accounts ac
[SqlHowTo].[dbo].PersonalDetails pd ON ac.AutoId = pd.PersonalDetailsId

In the above query, we are joining Accounts table of IHaveBeenRenamed database table and PersonalDetails of SqlHowTo table on AutoId of Accounts table and PersonalDetailsId of PersonalDetails table.

To know how to join two databases that are physically on different servers, read this post of DotNetFunda.com -


 Views: 1172 | Post Order: 62

Write for us