Solved

Inner Joining  3 tables from 3 different databases

Posted on 2010-01-07
5
226 Views
Last Modified: 2012-05-08
Does anyone have sample sql statement, using  -- Inner Joining  3 tables from 3 different databases  ?
Please

Very hard to find these types of examples...

Thanks
fordraiders
0
Comment
Question by:fordraiders
  • 2
  • 2
5 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 26200135
select d1.field, d2.field, d3.field
from
database1.dbo.tablename d1
join database2.dbo.tablename d2 on d1.field2 = d2.field2
join database3.dbo.tablename d3 on d2.field3 = d3.field3

make sense?
0
 
LVL 6

Accepted Solution

by:
hyphenpipe earned 250 total points
ID: 26200152
If all the databases are in the same instance then it would be:

select * from first_table f
inner join database_name..second_table s on f.[column_name] = s.[columns_name]
inner join database_name..third_table t on s.[column_name] = t.[columns_name]

If they are in separate instances then you would need to add them as linked servers and then it would be:

select * from first_table f
inner join linked_server.database_name.dbo.second_table s on f.[column_name] = s.[columns_name]
inner join linked_server.database_name.dbo.third_table t on s.[column_name] = t.[columns_name]
 
0
 
LVL 3

Author Comment

by:fordraiders
ID: 26200622
Yes, They are in the same "instance".

Here is what works for 2 tables from 2 different databases..

select SKU_VIEW.ITEM,
       SKU_VIEW.WWGMFRNAME,
       SKU_VIEW.WWGMFRNUM,
       WWGDESC_ALL.dbo.WwgDescRich.RICHTEXT
      from ITEM_DISPLAY.dbo.SKU_VIEW INNER JOIN WWGDESC_ALL.dbo.WwgDescRich ON
ITEM_DISPLAY.dbo.SKU_VIEW.ITEM = WWGDESC_ALL.dbo.WwgDescRich.ITEM


I would like to do the same with another table from another databases.

Database =   XrefInfo
Table name =  dbo.XrefDetail
pk column =     ITEM
Field I need in the query =  REDBOOKNUM

Sorry ,  Guess I should have added this to the question..

Thanks
fordraiders

 




0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 250 total points
ID: 26200705
select SKU_VIEW.ITEM,
       SKU_VIEW.WWGMFRNAME,
       SKU_VIEW.WWGMFRNUM,
       WWGDESC_ALL.dbo.WwgDescRich.RICHTEXT,
XrefInfo.dbo.Xrefdetail.Redbooknum
      from ITEM_DISPLAY.dbo.SKU_VIEW INNER JOIN WWGDESC_ALL.dbo.WwgDescRich ON
ITEM_DISPLAY.dbo.SKU_VIEW.ITEM = WWGDESC_ALL.dbo.WwgDescRich.ITEM
JOIN XrefInfo.dbo.Xrefdetail ON WWGDESC_ALL.dbo.WwgDescRich.Item = XrefInfo.dbo.Xrefdetail .Item
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 31674009
Thanks to all !
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question