Categories
Sql Server

How to kill All Running Processes in SQL Server

How to kill All Running Processes in SQL Server

In this post we will discuss How to kill All Running Processes in SQL Server. we do have solution by killing the running process to take a lock and perform any activity using a management command namely KILL & a system view sys.sysprocesses. However, what encourages me to write this solution is that sys.sysprocesses will be removed from the future version of SQL Server.

Query:

USE master
GO
DECLARE @SQL_String VARCHAR(MAX)
DECLARE @Database_id INT
 
--DO NOT forget to change database name in give below statement
SET @Database_id=DB_ID('Test')
SET @SQL_String=''
 
SELECT @SQL_String=@SQL_String + 'KILL '
+ COALESCE(CONVERT(VARCHAR(5),[session_id])+ '; ','')
FROM sys.dm_exec_sessions
WHERE database_id =@Database_id
 
--PRINT @SQL_String
EXEC (@SQL_String)