Solved

Inner Joining  3 tables from 3 different databases

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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