8

Is there a simple way to reconnect a query window that is "not connected" to the original connection that was used by that query without manually picking the server name needed? If I click the Connect button on the SQL Editor toolbar or right click -> Connection -> Connect... it defaults the server name to the last connection I used in that SSMS session, not to the last connection used by that specific query window.

After taking my laptop home at the end of the day, all the open windows will be set to not connected the next time I use it. Ideally, I'd like to be able to reconnect all to their original connections, but I don't know that even the one query at a time is possible.

  • In my experience, if you simply click "run" on your disconnected query window (highlight an innocuous portion of script, if necessary), it will first give you a connection error, then click it a second time, it will reconnect to the original server and run. – BradC Aug 14 '17 at 14:25
  • I removed my answer. Right click -> Connection -> Connect will also connect to last used server. But if you just run SELECT 1 in the window it should connect to same as it was connected to. – Daniel Björk Aug 14 '17 at 14:27
  • Now if you are talking picking the right database for the right server, for me it automatically defaults to the default database and so if your query was for a different database, then you have to select Options which will allow you to select the correct database – demongolem Sep 12 '18 at 20:18
11

In SSMS, if you run a query in a disconnected window, it will attempt to re-connect to the prior connection. You will get an error, then the window will reconnect. If you run a query like SELECT 1;, it will reconnect without having to do any data access/manipulation.

However, I'm a bit timid about relying on this to remember what server I was previously connected to. While I have noticed this to be the behavior, it is not documented, so I do not fully trust that it is "as designed" vs "the way it works." You should always make sure you are connected to the correct server before running any scripts.

  • This is my experience as well, although I normally only see this due to server outages or maintenance, not due to hauling my laptop home. – BradC Aug 14 '17 at 14:27
  • 1
    a handy alternative to SELECT 1 is to run the parse command via the toolbar, or CTRL-F5. This will not actually run any code, but will reconnect the session if it is disconnected. – Max Vernon Aug 14 '17 at 15:25
  • 1
    I have SQL Server Management Studio 14.0.17119.0 and it does not auto-reconnect. Some older version on my previous PC auto-reconnected successfully. – meir Dec 15 '17 at 9:07
  • @meir in SSMS 17, I've found its sometimes necessary to try twice and your session will reconnect the second time. – AMtwo Dec 15 '17 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.