How to kill a user session in SQL Server? The command is short. My first question in an interview is why someone wants to run it.

I’ve heard “kill all the sessions” as a response to a busy server. If those connections are doing the work we asked them to do, ending them all replaces a performance problem with interrupted users and transaction rollbacks. I’d identify the particular session and its activity first.
Find the session
This read-only query lists user sessions, excludes the connection running it, and shows any current request. The proposed command is text for review. The query does not end a connection.
SELECT s.session_id,
s.login_name,
s.host_name,
s.program_name,
s.status,
r.command,
r.blocking_session_id,
CONCAT('KILL ', s.session_id, ';') AS ProposedCommand
FROM sys.dm_exec_sessions AS s
LEFT JOIN sys.dm_exec_requests AS r
ON r.session_id = s.session_id
WHERE s.is_user_process = 1
AND s.session_id <> @@SPID
ORDER BY s.session_id;Check the login, application, host, request, and transaction context. Then decide whether ending one connection is justified. Session IDs can be reused after a connection ends, so identify the session again immediately before acting.
End one reviewed connection
For a chosen session, the command is KILL followed by its numeric session ID. For example, if you have just verified session 57 in a lab:
-- Example only. Verify the current session ID before running.
KILL 57;SQL Server rolls back an open transaction belonging to that connection. A large rollback can take time. During a rollback, KILL 57 WITH STATUSONLY reports progress without issuing a second termination request. You also need permission to alter a connection.
My original script concatenated a KILL command for every session with an ID above 50 and executed the whole string. I wouldn’t hand that script to a candidate or run it on a busy instance. The numeric cutoff doesn’t describe business intent, and it doesn’t tell us which connection caused the incident. If the actual goal is exclusive access to one database, the single-user database procedure addresses a different, database-scoped task.
The better interview answer starts with the reason, shows the sessions, and ends only the connection that needs intervention. I’d hire that reasoning before I’d hire a memorized mass-kill script.
References: Microsoft KILL documentation and sys.dm_exec_sessions.
Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.





2 Comments. Leave new
But killing processes is important part of admin’s world. Have this situation when there are apps consuming a lot of data for instance with unlimited date ranges on really big TBs databases we have. We as DBAs educate devs to write optimized queries and put there some boundaries especially for very complex realtime calculations in OLTP systems. But management says no there won’t be any issue, put it into production, we have big HW it will handle it. So you have to let it pass into production, there starts to be performance issues and management comes to you saying, ok you were right, kill it and we will reimplement it and replace it. But first you have to let it go because sometimes management doesn’t listen to technical arguments and simply they think they are right because they are managers and you can do nothing about it. This is the real life and that is why killing all app’s related SPIDs comes handy sometimes.
I a similar light as Jan posted, as a DBA I find many runaway queries… in those cases I only kill the one SPID(s)… I rarely kill all.
Now, in the event I am forced to kill all SPIDS connecting to a specific database, say in a ROLLBACK situation after a deployment goes bad; I have found that using the SINGLE_USER can backfire very quickly if an application is connecting in the background using a privileged user (in some cases “sa”). In a situation like that you have successfully locked yourself out of the database until someone can bring down the application… and in my experience, good luck with that!
To overcome this issue, I use:
ALTER DATABASE <> SET OFFLINE WITH ROLLBACK IMMEDIATE;
WAITFOR DELAY ’00:00:30′; — 30 seconds is usually enough to force a timeout on any application, but you can set to your desired number or remove altogether if you just want to perform a quick “reset”.
ALTER DATABASE <> SET ONLINE;
… rest of your code…
This way… all connections will be guaranteed to disconnect regardless of privilege.
Kind Regards