query windows are tied to server connection. you can change the database (use database) from the drop down. to change the connection, in the query window right click and pick:
connection->change connection
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I will often be working with queries with multiple servers and databases open in SSMS at the same time. When I run a query, I know I can use [DATABASE] to run in a specific, but SS seems to get confused about which SERVER I want to run on. I'm logged into several different servers in SSMS (all with different credentials). How can I FORCE my query to execute ON SERVER-A using DB-B?
query windows are tied to server connection. you can change the database (use database) from the drop down. to change the connection, in the query window right click and pick:
connection->change connection
Hi @Dom
You could go to SQLCMD mode in an SSMS query Window (menu bar Query-->SQLCMD mode) using SQL command :CONNECT
to connect other server and USE
to specify database.
Check this example:
:CONNECT Server1
Use Database_A
Select * from Table1
GO
:CONNECT Server2
Use Database_B
Select * from Table2
GO
:CONNECT Server3
Use Database_C
Select * from Table3
GO
Note that the query window connection reverts back to the initial connection after each GO.
Best regards,
Cosmog Hong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.