Results 1 to 17 of 17

Thread: QUERY on two different servers

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    QUERY on two different servers

    If I have two SQL Server connections open to two different servers...can I run a query, for instance a INNER JOIN on tables on seperate servers?

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

    Re: QUERY on two different servers

    YEs.... and no.... or rather no and yes.

    You can, but the user context that is running the query will have to have acces to both eservers.

    If I remember right.... hte syntax is:

    Server.Database.dbo.TableName

    -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??? *

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: QUERY on two different servers

    Apprently I need to do linked servers to do this?

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

    Re: QUERY on two different servers

    I don't know for 100% sure.... but any time I've needed to do it, it has been through linked server connection, so I assume that one does.

    -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
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: QUERY on two different servers

    We have also always used LINKED SERVERS - even to go to obscure places like PERVASIVE...

    *** 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

  6. #6
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: QUERY on two different servers

    Another way of doing this would be to use OPENDATASOURCE function. You should be able to check Books Online for a sample.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

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

    Re: QUERY on two different servers

    I believe I used OPENDATASOURCE for that PERVASIVE DB but still needed the DSN connection string to be loaded as a LINKED SERVER - I will check when I get into work later...

    *** 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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: QUERY on two different servers

    That would be awesome if yall could check on this for me.

  9. #9
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: QUERY on two different servers

    Quote Originally Posted by mlosso
    That would be awesome if yall could check on this for me.
    A simple example would be
    PHP Code:
    SELECT   *
    FROM      OPENDATASOURCE(
             
    'SQLOLEDB',
             
    'Data Source=ServerName;User ID=MyUID;Password=MyPass'
             
    ).Northwind.dbo.Categories 
    As taken from BOL.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: QUERY on two different servers

    Ya I saw that on MSDN. Working with it now, can't seem to get it work just right yet.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: QUERY on two different servers

    Is it possible that OPENDATASOURCE CAN BE BLOCKED?

  12. #12
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: QUERY on two different servers

    I don't know of whether it can be blocked. I don't think so. If you are using Right userId and Password then you should be able to use it properly.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

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

    Re: QUERY on two different servers

    Have you tried LINKED SERVER yet?

    Go into ENTERPRISE MANAGER - under the SECURITY branch in the object panel on the left side open LINKED SERVERS - you can right-click on that to add a NEW LINKED SERVER.

    Fill in the various text boxes - security and what not - and that server will appear available to you.

    For example - we linked to a SERVER called STRATACS - then in a QUERY in our regular local database/table we were able to:

    SELECT * FROM STRATACS.TVBD.DBO.PARTYLOG.

    Using LINKED SERVERS allows you to access tables with standard SELECTS and JOINS - that's a very nice benefit over OPENDATASOURCE.

    For our PERVASIVE DB that we did use OPENQUERY with we also LINKED servers. Since it was not SQL SERVER we selected OTHER DATA SOURCE and chose OLEDB PROVIDER FOR ODBC as the PROVIDER NAME.

    Then in STORED PROCEDURES we could:

    SELECT * FROM OPENQUERY(LINKNAME, 'SELECT...')

    This executes the 'SELECT ...' on the ODBC connection to that PERVASIVE DB.

    *** 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

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: QUERY on two different servers

    Reason I have not done Linked Server is because I must request access to do it, which could take some time. I need to do the OPENDATASOURCE using Windows Authentication...doesnt seem to be working..

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

    Re: QUERY on two different servers

    Should be a matter of specifying INTEGRATED SECURITY instead of that USERNAME or PASSWORD...

    *** 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

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: QUERY on two different servers

    Thats what I thought, but when I do specify SSPI, it says server cannot be found when I clearly am connected to it in Enterprise Manager...any special lashes or backslashes?

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

    Re: QUERY on two different servers

    This works:

    Code:
    Select *
       From OpenDataSource('SQLOLEDB'
                 ,'DATASOURCE=servername;INTEGRATED SECURITY=SSPI').dbname.dbo.tablename

    *** 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