Created
February 1, 2012 15:44
-
-
Save benjaminramey/1717627 to your computer and use it in GitHub Desktop.
This script will kill all current connections to a database in MSSQL. This is useful for detaching the database, restoring it or performing other tasks that require all connections dropped from the database.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DECLARE @SQL VARCHAR(8000) | |
| SELECT @SQL=COALESCE(@SQL,'')+'Kill '+CAST(spid AS VARCHAR(10))+ '; ' | |
| FROM sys.sysprocesses | |
| WHERE DBID=DB_ID('<DATABASE_NAME>') | |
| EXEC(@SQL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment