Link to home
Start Free TrialLog in
Avatar of Glenn Stearns
Glenn StearnsFlag for United States of America

asked on

Join tables from different databases on same SQL server

I have three tables, one from one database and two from another, both databases on the same SQL server:  (The server name is M2MSERVER)
INMASSINVTXN.dbo.INMASSTXNS
m2mdata03.dbo.inmast
m2mdata03.dbo.inonhd

I want a SELECT statement to select certain fields from each of the three tables, the common field being ITEM_NO. The three tables have no other common field.
Each m2mdata03.dbo.inmast record can have multiple matches in both INMASSINVTXN.dbo.INMASSTXNS and m2mdata03.dbo.inonhd.
The result would, in effect, join the two m2mdata03 tables first, creating a combination record for every ITEM_NO in inonhd that has a matching ITEM_NO record in inmast.
Then it would, in effect, join that result for every record in INMASSTXNS that has a matching ITEM_NO in the inmast/inonhd result.

I know how to make this work when it involves only two tables and both are in the same database, but not how to deal with three tables and two databases in an SQL SELECT and JOIN statement.

Thanks for the help!


ASKER CERTIFIED SOLUTION
Avatar of BrandonGalderisi
BrandonGalderisi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Glenn Stearns

ASKER

Thanks...as usual, I was trying too hard to write my script! Yours works great - really appreciate the quick reply!