Created
November 7, 2014 04:31
-
-
Save hoangitk/e6fd135cce955e0230b1 to your computer and use it in GitHub Desktop.
Kill all connections to SQL Server
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
| USE master | |
| go | |
| DECLARE @dbname sysname | |
| SET @dbname = '<your database name>' | |
| DECLARE @spid int | |
| SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname) | |
| WHILE @spid IS NOT NULL | |
| BEGIN | |
| EXECUTE ('KILL ' + @spid) | |
| SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname) AND spid > @spid | |
| END | |
| -- check is there any connection again? | |
| select * from master.dbo.sysprocesses where dbid = db_id(@dbname) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment