Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pedrovasconcellos/3a3f6b77fdfafcf4abcd63a7c44c5859 to your computer and use it in GitHub Desktop.
Save pedrovasconcellos/3a3f6b77fdfafcf4abcd63a7c44c5859 to your computer and use it in GitHub Desktop.
SQL to close all connections and drop database
--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