T-SQL – Find running queries

Here’s a little script in T-SQL to find queries running in our SQL SERVER database:

SELECT sqltext.TEXT,
    res.session_id,
    res.status,
    res.command,
    res.cpu_time,
    res.total_elapsed_time
FROM sys.dm_exec_requests res
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext

This script is also useful in the case that a query is in loop or it is putting too much time and our intention is to stop it.

Once the query is launched and located, you just run the KILL command with the session ID of the query.

KILL [session_id]

Leave a Comment

Your email address will not be published. Required fields are marked *