1

In SQL Server Management Studio, is there any way (for example a SQLCMD command or something) to get the SQL script window to automatically disconnect as soon as the query has finished executing?

2 Answers 2

5

Easy one ...

Think about why do you want to do it ? Any specific reasons ?

From BOL :

To change the options for the current queries, click Query Options on the Query menu or the shortcut menu of the SQL Server Query window. Under Execution, click Advanced.

Disconnect after the query executes

When this check box is selected, the connection to SQL Server is terminated after the query completes. This check box is cleared by default.

enter image description here

1
  • Would love to know how to do this by way of SQL query, so you could do it for only one command instead of all of them. Nov 26, 2014 at 12:03
0

Add the line use master at the bottom of your query window, which will release the connection used by the previous lines. Then you can do whatever you want to your db, regen, move, backup, etc... without needing to close the query window. This is quite useful for testing scenarios where you don't want to reload the query window every single iteration.

use TestDatabase;

select * from Somewhere
select * from SomewhereElse
selecd * from NowhereInParticular

use master

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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