After executing this query on master db ,it is giving me all running process on all databases, is there any query which will kill all process running on a database .
USE
Master
GO
SELECT
SPID,DBID FROM SYSPROCESSES
WHERE
DBID NOT IN (1,2,3,4) AND SPID >50 AND SPID<> @@spid
If you want to force every other connection to disconnect, and you have suitable permissions, you can bounce the database in and out of single user mode:
alter database current set single_user with rollback immediate;
go
alter database current set multi_user;
go
Any other connection to the same database will be terminated.
Please see below SQL script to kill all processes for a given Database (SQL Server 2014)
Declare @DbName nvarchar(60)='YourDBName'
Declare @SPID int
--get all processes
DECLARE @Table TABLE(
SPID INT,
Status VARCHAR(MAX),
LOGIN VARCHAR(MAX),
HostName VARCHAR(MAX),
BlkBy VARCHAR(MAX),
DBName VARCHAR(MAX),
Command VARCHAR(MAX),
CPUTime INT,
DiskIO INT,
LastBatch VARCHAR(MAX),
ProgramName VARCHAR(MAX),
SPID_1 INT,
REQUESTID INT
)
INSERT INTO @Table EXEC sp_who2
--using cursor to kill all processes
Declare cur_KillAllProcesses CURSOR FAST_FORWARD FOR
Select
SPID
From @Table
WHERE DBName=@DbName
OPEN cur_KillAllProcesses
FETCH NEXT FROM cur_KillAllProcesses INTO
@SPID
WHILE @@FETCH_STATUS=0
BEGIN
--add kill process command
Exec('KILL '+ @SPID)
FETCH NEXT FROM cur_KillAllProcesses INTO @SPID
END
CLOSE cur_KillAllProcesses
DEALLOCATE cur_KillAllProcesses
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With