Results 1 to 7 of 7

Thread: Cross Database Queries

  1. #1
    Join Date
    Oct 2006
    Posts
    38
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Cross Database Queries

    Is it possible to do a cross database query. Both db are on the same server. If it is can you give me an example.

    Thanks.

    Josh

  2. #2
    Join Date
    Jul 2006
    Posts
    53
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Yes you can. For example if you had Orders and Products tables in different databases (not sure why you'd do this) and both belonged to the dbo schema, you could do a query like this:

    select * from [db1].[dbo].[Orders] o, [db2].[dbo].[Products]
    where o.productId = p.ProductId

    Basically you just need to fully qualify the names of your SQL objects.

    A word of warning though -it's difficult to maintain referential integrity across databases so only do this if you have a good reason to.

  3. #3
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    ...that isn't possible on our servers though as each database has its own logins.
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  4. #4
    Join Date
    Jan 2006
    Posts
    419
    Thanks
    2
    Thanked 16 Times in 16 Posts

    Default

    I guess you could manually grant access to both databases for a particular user.

    Alternatively in SQL 2005 there is something called linked servers - unfortunately you are unlikely to have the rights to setup a linked server though and whether you could use it to access two databases on the same server as it is normally for two databases on seperate servers.

    I am not sure why you would want to query data accross two databases - I would have thought it would be best to copy the data in so it could be queried.

  5. #5
    Join Date
    Mar 2005
    Location
    Hampshire
    Posts
    432
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default

    Take a look at

    http://msdn.microsoft.com/en-us/library/ms190312.aspx

    Code:
    SELECT a.*
    FROM OPENROWSET('SQLNCLI', 'Server=Seattle1;Trusted_Connection=yes;',
         'SELECT GroupName, Name, DepartmentID
          FROM AdventureWorks.HumanResources.Department
          ORDER BY GroupName, Name') AS a
    However, I would NEVER recommend cross database queries if you can avoid it.

    Just chill

  6. #6
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Quote Originally Posted by Tanzy View Post
    Take a look at

    http://msdn.microsoft.com/en-us/library/ms190312.aspx

    Code:
    SELECT a.*
    FROM OPENROWSET('SQLNCLI', 'Server=Seattle1;Trusted_Connection=yes;',
         'SELECT GroupName, Name, DepartmentID
          FROM AdventureWorks.HumanResources.Department
          ORDER BY GroupName, Name') AS a
    However, I would NEVER recommend cross database queries if you can avoid it.
    ...interesting, didn't know you could specify a connection string within a cross-db query like that.
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  7. #7
    Join Date
    Jan 2008
    Location
    Copenhagen
    Posts
    42
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    you can always perform the 2 queries separated, populate a DataTable inside a DataSet and then use the DataSet to group, order by, filter, etc ...

    Code:
    DataSet ds = new DataSet();
    ds.Table[0] = getDataTableFromQuery( query1 );
    ds.Table[1] = getDataTableFromQuery( query2 );
    DataColumn parentColumn = ds.Tables[0].Columns["CustID"];
    DataColumn childColumn = ds.Tables[1].Columns["CustID"];
    
    DataRelation relCustOrder;
    relCustOrder = new DataRelation("CustomersOrders", parentColumn, childColumn);
    ds.Relations.Add(relCustOrder);
    etc ...
    Bruno Alexandre
    Strøby, Danmark

    "a Portuguese in Denmark"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Database backup
    By heavenlyarts in forum Technical Support
    Replies: 4
    Last Post: 13th September 2007, 08:17 PM
  2. Database offline
    By thinkingaustralia in forum Technical Support
    Replies: 7
    Last Post: 12th September 2007, 03:48 PM
  3. Database
    By nikkidodd in forum Customer Feedback and Suggestions
    Replies: 0
    Last Post: 11th November 2006, 08:17 PM
  4. Database Backup
    By andyj in forum MSSQL
    Replies: 2
    Last Post: 23rd March 2006, 10:30 AM

Bookmarks

Posting Permissions

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