Results 1 to 8 of 8

Thread: Multiple Join To Different DB

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Multiple Join To Different DB

    I'm using MS SQL Server 2000 here & the plan is to connect 2 tables from database1 to the same table in database 2 Um, can i have some help to pick apart the below statement so it'll work please????
    Code:
    SELECT Table1.Col1,  
    	   Table2.Col1 
    FROM   Table1  
    INNER JOIN DB2.TableA AS TBL1 ON Table1.KeyCol = TBL1.KeyCol
    INNER JOIN DB2.TableA AS TBL2 ON Table2.KeyCol = TBL2.KeyCol
    WHERE Table1.KeyCol = 1

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Multiple Join To Different DB

    The full path to any SQL Server object is Server.Database.Owner.Object.

    In your sample code DB2 would be the Owner. Simply add another period

    INNER JOIN DB2..TableA

    or specify the owner as well

    INNER JOIN DB2.dbo.TableA

  3. #3

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: Multiple Join To Different DB

    Hi Bruce!

    I tried adding DB2.dbo.Table1 before & have now tried with just the extra dot since you put that, but still no luck i'm afraid!

    Thanks,
    Alex

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016

    Re: Multiple Join To Different DB

    Does the user trying to join the table in the second database have access to the second database?

    Are you getting an error message? Also, you reference a Table2, but it's not listed as a table in your From clause.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Multiple Join To Different DB

    Do you have both the Pubs and Northwind database on your server?

    This query should work when logged into the Pubs database

    Select fname, lname, db2.lastname
    From Employee
    Inner Join Northwind..Employees DB2 On db2.employeeid = Employee.job_id

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Multiple Join To Different DB

    Whenever I am going to do cross database joins, I use the little trick in QUERY ANALYZER where you RIGHT CLICK on the TABLE, drag it into the QUERY pane and let go of the mouse. Choose SELECT from the drop-down menu.

    Doing that gets you the fully qualified name of the database - one that is guaranteed to work.

  7. #7

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: Multiple Join To Different DB

    Hi Guys! Thanks for the help so far! Ok to take your sample bruce with the pubs/northwind databases, here's what i'm trying to do:
    Select employee.fname, employee.lname,
    jobs.job_id,
    db2.lastname
    From Employee
    Inner Join Northwind..Employees DB2 On db2.employeeid = Employee.job_id
    RIGHT OUTER JOIN Northwind..Employees DB3 on db2.employeeid = jobs.job_id

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  8. #8

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: Multiple Join To Different DB

    Ok got it thanks! Here's the sort of thing i was trying to get. Many thanks again for those suggestions to help me find this! Much appreciated!
    SELECT DB1_tbl1.[job_id], DB1_tbl2.[lorange]
    FROM [pubs]..[jobs] AS DB1_tbl1
    INNER JOIN [Northwind]..[Employees] AS DB2_tbl1 ON DB1_tbl1.[job_id] = DB2_tbl1.[employeeid]
    LEFT OUTER JOIN [pubs]..[roysched] AS DB1_tbl2 ON DB2_tbl1.[employeeid] = DB1_tbl2.[lorange]

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width