Results 1 to 6 of 6

Thread: Joining Tables From Diff Databases

  1. #1

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Joining Tables From Diff Databases

    Hi there,

    i have 2 databases one and MSDE and the other a SQL SERVER database.

    I would like to be able to join a table in my MSDE table (Stock) to a table in the SQL Server(Products)

    how is this possible, or is it?

    can this be done as a stored procedure?
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  2. #2
    Member Dennis DVR's Avatar
    Join Date
    Jul 2005
    Location
    Manila Philippines
    Posts
    54

    Re: Joining Tables From Diff Databases

    If you're just going to do this only once you can use the OPENROWSET or OPENDATASOURCE look at your SQL Server Books Online regading these functions.

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

    Re: Joining Tables From Diff Databases

    Should be possible. I have only ever done this from SQL Server to SQL Server. There are two methods.

    Setup a Linked Server. Then you can use the 4 part name syntax to access the server.

    Code:
    Select * 
    From LocalTable L Left Join LinkedServerName.DataBaseName.OwnerName.TableName LS
    On L.Id = LS.Id
    Use the Ad-Hoc query methods - OpenRowset, OpenDataSource, OpenQuery

    Code:
    SELECT Stock.*, P.ProductName 
    FROM Stock
    INNER JOIN OPENROWSET('SQLOLEDB','servername';'sa';'sapwd',
       'SELECT ProductId, ProductName FROM Products') AS P
       On Stock.ProductId = P.ProductId

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Joining Tables From Diff Databases

    Quote Originally Posted by brucevde
    Should be possible. I have only ever done this from SQL Server to SQL Server. There are two methods.

    Setup a Linked Server. Then you can use the 4 part name syntax to access the server.

    Code:
    Select * 
    From LocalTable L Left Join LinkedServerName.DataBaseName.OwnerName.TableName LS
    On L.Id = LS.Id
    Use the Ad-Hoc query methods - OpenRowset, OpenDataSource, OpenQuery

    Code:
    SELECT Stock.*, P.ProductName 
    FROM Stock
    INNER JOIN OPENROWSET('SQLOLEDB','servername';'sa';'sapwd',
       'SELECT ProductId, ProductName FROM Products') AS P
       On Stock.ProductId = P.ProductId
    Actualy, doesn't the Linked Server superceede the need to use the 4 part name? I've know I've gone from Server A to Server B using the 4 part harmony, with out a linked server.... I thought a Linked Server wasa "shortcut" of sorts, allowing you to LS.Table .... ?? but it's been a while, so I could be smoking something...

    ;P

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: Joining Tables From Diff Databases

    You are smoking something again (and probably drinking too).

    If you leave out the server name, SQL Server assumes the current server. But then again things change so fast, I am becoming obsolete (so says the junior prog at work - l'il *******)

  6. #6
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232

    Re: Joining Tables From Diff Databases

    how do you set up a linked server?

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