sql query to two databases

  • I have tables in one database with a link being user_id field to other tables in another database.

    how do you connect two different databases to one query.

    ie databaseA and databaseB

  • Trout (7/14/2009)


    I have tables in one database with a link being user_id field to other tables in another database.

    how do you connect two different databases to one query.

    ie databaseA and databaseB

    the key is to use the fully qualified names of the tables in each of the databases

    SELECT *

    FROM databaseA.dbo.AllUsers FirstAlias

    LEFT OUTER JOIN databaseB.dbo.AllUsers SecondAlias

    ON FirstAlias.user_id = SecondAlias.user_id

    note you could do the same on a table on a different server, if you added that server as a linked server...then you would have a 4 part name:

    ServerName.DatabaseName.dbo.Tablename, for example.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply