Created
September 4, 2019 04:15
-
-
Save pedrovasconcellos/3a3f6b77fdfafcf4abcd63a7c44c5859 to your computer and use it in GitHub Desktop.
SQL to close all connections and drop 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
| --SQL to close all connections and drop database | |
| USE [master]; | |
| DECLARE @DatabaseName VARCHAR(MAX) = 'MyDatabaseName' --to change | |
| DECLARE @kill VARCHAR(8000) = ''; | |
| SELECT @kill = @kill + 'kill ' + CONVERT(VARCHAR(5), session_id) + ';' | |
| FROM sys.dm_exec_sessions | |
| WHERE database_id = db_id(@DatabaseName) | |
| EXEC(@kill); | |
| DROP DATABASE DatabaseName --to change |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment