Run Query with Different Server and Different Database

Mike 341 Reputation points
2021-02-16T21:09:55.527+00:00

Hi - How I can run the following same query in different servers and database. How we can we use the 'USE' command with Server to mover server.

Use C_Server1
Use Per_A
Select * from YTR where surname like '%Test%'

Use C_Server2
Use Cer_A
Select * from YTR where surname like '%Test%'

Use C_Server3
Use Uer_F
Select * from YTR where surname like '%Test%'

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,559 questions
0 comments No comments
{count} votes

Accepted answer
  1. Dan Guzman 9,211 Reputation points
    2021-02-16T21:25:28.417+00:00

    USE switches database context on the current connection. You can use SQLCMD mode in an SSMS query Window (menu bar Query-->SQLCMD mode) in order to use SQLCMD commands like :CONNECT in the example below. Note the the query window connection reverts back to the initial connection after each.

    :CONNECT C_Server1  
    Use Per_A  
    Select * from YTR where surname like '%Test%'  
    GO  
      
    :CONNECT C_Server2  
    Use Cer_A  
    Select * from YTR where surname like '%Test%'  
    GO  
      
    :CONNECT C_Server3  
    Use Uer_F  
    Select * from YTR where surname like '%Test%'  
    GO  
    
    2 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. MelissaMa-MSFT 24,176 Reputation points
    2021-02-17T01:35:56.19+00:00

    Hi @Mike ,

    You could also have a try with Linked Servers.

    Linked servers offer the following advantages:

    • The ability to access data from outside of SQL Server.
    • The ability to issue distributed queries, updates, commands, and transactions on heterogeneous data sources across the enterprise.
    • The ability to address diverse data sources similarly.

    You could get to them in SSMS from the following location in the tree of the Object Explorer:

    Server Objects-->Linked Servers

    or you can use sp_addlinkedserver.

    Then you could query as follows:

    select * from LinkedServerName.DatabaseName.OwnerName.TableName  
    

    After you created two linked servers in C_Server1, then you could use below query:

    Use C_Server1  
    go  
      
    select * from [Per_A].[dbo].[YTR] where surname like '%Test%'  
    go  
      
    select * from [C_Server2].[Cer_A].[dbo].[YTR] where surname like '%Test%'  
    go  
      
    select * from [C_Server3].[Uer_F].[dbo].[YTR] where surname like '%Test%'  
    go  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Alberto Poblacion 1,556 Reputation points
    2021-02-16T21:17:26.627+00:00

    The USE command only works for moving between databases within the same server instance.
    It is not possible to move to another server by means of a command because the other server will need another connection. Your existing connection, the one that you are using for sending the commands, is connected to one specific server. You cannot send a command to that server to open the connection to a different server, because the connection has to be opened by the client application that you are using for sending the commands, not by the target server that is currently receiving the commands.

    0 comments No comments