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
INNER JOIN
[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 -

http://www.dotnetfunda.com/forums/show/3006/linking-2-servers-andcopying-table-data-from-server1-to-server2

 Views: 19456 | Post Order: 62



Write for us






Hosting Recommendations