Results 1 to 4 of 4

Thread: [RESOLVED] Cross Database Queries

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Resolved [RESOLVED] Cross Database Queries

    Hi,

    VB Code:
    1. SELECT Orders.OrderID, Orders.DealerID, Dealers.Dealer, Orders.OrderDate, Orders.ShipDate, Orders.Cancelled, Dealers.DealerType
    2. FROM Dealers INNER JOIN Orders ON Dealers.ID = companysql.AcornInternational.Orders.NewDealerID
    3. WHERE (((Orders.ShipDate)>GetDate()) AND ((Orders.Cancelled)=0) AND ((Dealers.DealerType)="Export"));

    I have the following sp Select Im trying to set up. The problem is the sp will Reside on the same server (company sql) but in a different database from one of the tables required (Orders).

    How do I cross Query the table Orders from AcornInternational database?

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

    Re: Cross Database Queries

    Simply use fully qualified names...

    Code:
    Select * From [Database1].[dbo].[TableNameA] TA
       Left Join [Database2].[dbo].[TableNameX] TX on TX.KeyCol=TA.KeyVal
    If you are in query analyzer you open the object browser - open the branches till you see your table names listed...

    Then right-click a table and drag it into the query window - popup menu - take the SELECT option...

    You will see what the fully-qualified names are for you table with that...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Cross Database Queries

    Hmm I don't have query analyser available as im using SQL 2005 Business Intelligence Delelopment Studio and it doesnt seem to just let me drag a table from Server Explorer into the Designer window? Can this be done?

    This worked by the way thanks.
    VB Code:
    1. FROM Dealers INNER JOIN AcornInternational.dbo.Orders ON Dealers.ID = Orders.NewDealerID

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

    Re: Cross Database Queries

    I have SQL Server Management Studio that I use with MS SQL 2005 db's...

    It works differently then the old QA I'm used to.

    If I right-click a table in the Object Explorer I get a menu with "Script Table As". Select that option...

    Give me Create to... Drop to... Select to... Insert to... Update to... Delete to...

    I can mouse-over any of those and choose:

    New Query Editor Window

    That shows a fully qualifed name for tables and fields...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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