Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2009
    Posts
    18

    Unanswered: How to connect to server via SQL commands?

    During doing adhoc query in SQL Server Management studio, I can switch to another data base with the "use" SQL command:

    use <database_name>
    ... blah, blah, blah...

    What if I want to switch to another database server?

    I know I can click some buttons to do so, but I wish to do that with a SQL command. Something like:

    "connect to server_name"
    use <database_name>
    ... blah, blah, blah...

    What's the syntax for the "connect to server_name" command? Thanks!

  2. #2
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    There is none, as far as I know.

  3. #3
    Join Date
    Oct 2009
    Posts
    18
    I don't understand this...

    We have sp_addserver to "define" or "add" a server but no way to connect to the newly defined or added server?

  4. #4
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    Maybe you should read up on that command a little more. That is to add a linked server on the server itself. It does nothing for a client connection, which is what you are trying to do.

  5. #5
    Join Date
    Jun 2004
    Location
    Long Island
    Posts
    696
    Quote Originally Posted by knhcmp View Post
    During doing adhoc query in SQL Server Management studio, I can switch to another data base with the "use" SQL command:

    use <database_name>
    ... blah, blah, blah...

    What if I want to switch to another database server?

    I know I can click some buttons to do so, but I wish to do that with a SQL command. Something like:

    "connect to server_name"
    use <database_name>
    ... blah, blah, blah...

    What's the syntax for the "connect to server_name" command? Thanks!

    Create a linked server, then to query a table

    select * from [linkedservername].database.owner.table or

    Also, look at OpenRowSet and OpenQuery functions.

  6. #6
    Join Date
    Oct 2009
    Posts
    18
    OK, so there's no adhoc SQL command to connect/reconnect to a server.

    Has anyone spun their own script or even program/DLL to do this?

  7. #7
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    There's a change connection button in Management Studio...
    Look at the button on the left hand side of the database list drop down.
    George
    Home | Blog

Posting Permissions

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