Skip to content

Instantly share code, notes, and snippets.

@phenixita
Created February 12, 2019 08:40
Show Gist options
  • Save phenixita/be5fa8f128f034e6180f6b5e5f9be5ed to your computer and use it in GitHub Desktop.
Save phenixita/be5fa8f128f034e6180f6b5e5f9be5ed to your computer and use it in GitHub Desktop.
Drop and create database with kill processes
USE master
GO
-- Creating the ModernApiDB
DECLARE @dbname sysname
SET @dbname = 'MY_DB'
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
GO
DROP DATABASE MY_DB
GO
CREATE DATABASE MY_DB
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment