Skip to content

Instantly share code, notes, and snippets.

@juanonsoftware
Created April 12, 2018 09:43
Show Gist options
  • Save juanonsoftware/9d0709c2a5f88ed6c495ae7663d4a96d to your computer and use it in GitHub Desktop.
Save juanonsoftware/9d0709c2a5f88ed6c495ae7663d4a96d to your computer and use it in GitHub Desktop.
Kill blocking processes on a database
--- Kill blocked processes on a database
DECLARE @databasename nvarchar(50)
SET @databasename = N'Datbase_Name'
-- SET @databasename = DB_NAME()
DECLARE @Sql varchar(max)
SET @Sql = ''
SELECT @Sql = @Sql + 'Kill ' + Convert(varchar, SPId) + ';'
FROM master.dbo.sysprocesses
WHERE DBId = DB_ID(@databasename) AND SPId <> @@spid
and spid IN (SELECT blocked FROM master.dbo.sysprocesses)
--You can see the kill Processes ID
SELECT @Sql
--Kill the Processes
EXEC(@Sql)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment