Solved

Inner Joining  3 tables from 3 different databases

Posted on 2010-01-07
5
196 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL FUNCTION TO Capitalize Each Word in a String 10 61
query help 18 52
SQL Help - 12 42
t-sql splitting string column 5 27
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now