SQL Server – Loop Inside Stored Procedure Doesn’t Stop After Query Cancellation

sql serverstored-procedures

I have a few nested stored procedures, which have a while loop at the end. When I stop the query, the loop keeps running, though. Every child sp is independant from another and has a duration of less than 1s. Any ideas why it keeps running or how to properly cancel it?

Best Answer

You could have a look at running requests with either sp_whoisactive or with the below query and kill the session associated with the query / stored procedure.

select r.session_id
    , s.text
from sys.dm_exec_requests r
    cross apply sys.dm_exec_sql_text(sql_handle) AS s

Then you just execute the KILL command, like below, with the appropriate integer value from the above result, session which is associated with your stored procedure / query:

KILL <session_id>