Shortcut: Change connection in SQL Server Management Studio

I commonly run into a few connection-related scenarios:

  • I'm working on a large query and need to run it against several servers, not concurrently, but one after the other.
  • I've just made a database connection, and got my query ready, only to discover that I've connected to the wrong server.

Either way, what I've seen people do in these scenarios is to:

  • Select all the text in the current query
  • Copy it
  • Open a new query window
  • Paste the code

That's all good but SQL Server Management Studio (SSMS) has had a simpler way of doing this for quite a while.

If you right-click in the middle of a query window, you can choose to change the connection. Here's an example. I opened a new query window and entered a query, only to find that it connected to the Azure server that I was working with earlier. The easiest option is to right-click and choose to change the connection:

After you click Change Connection, you can log on again:

And then I can continue in the same query window as before. This is particularly useful if I need to run some code against a test server, and once I've decided that it was correct, I can just change the connection and connect to the "real" server.

At the bottom-right of your query window, you can always see which server you are connected to:

And at the bottom-left, you can see your connection state:

4 thoughts on “Shortcut: Change connection in SQL Server Management Studio”

    1. Hi Jon, yes, but only with T-SQL scripts in SQLCMD mode. Then you can use the ::CONNECT statement to connect to another server.

        1. I would imagine so. In the end, the app (SSMS) is just closing the current connection and opening another one and reconnecting the window to it. All those actions must be programmable or it wouldn't work now. Sounds challenging though.

Leave a Reply

Your email address will not be published. Required fields are marked *