How to Kill SQL Threads or Kill User Sessions

0


How to Kill SQL Threads or Kill User Sessions

Method-1

This is one of best and quickest way to Kill All Threads or Kill All User Sessions or Kill All Sessions

Sometime you may get error while restoring a database that action cannot be performed as user’s sessions are active on the database. If it is important to restore and killing database user’s session won’t harm much, then you can use below script. This will temporally change the access mode to single user and kill sessions, and then revert it to multi user mode.

Note: This script will kill all users session associated with a database. In this script I have demonstrated AdventureWorks database.

USE master;

GO

ALTER DATABASE AdventureWorks

SET SINGLE_USER

WITH ROLLBACK IMMEDIATE;

ALTER DATABASE AdventureWorks

SET MULTI_USER;

GO


Method-2

Let’s say you want to kill all processes by a particular user or account, then you can use below script to find the related spid for all the processes initiated by user or account. Here my account is HABIB\Administrator

SELECT spid from master..sysprocesses

WHERE loginame = ‘HABIB\Administrator’


Once you get the spid details, you can simply execute KILL command against those processes.

Command is KILL <spid>

 

For more article updates, videos and posters join our official page in Facebook

Facebook Page: https://www.facebook.com/ServerGeeks

Web Site: https://servergeeks.wordpress.com/

Video Channel: https://www.youtube.com/user/Habibmvp