Today, we have been working on a performance issue where our customer is running a stored procedure but we don't know exactly what is the part of this that is running.
Let me show you an example how to obtain this information:
CREATE PROCEDURE WhereIsTheQueryIsRunningNow
AS
BEGIN
SELECT 1
SELECT 2
WAITFOR DELAY '00:00:03'
SELECT 3
SELECT 4
END
SELECT
req.session_id
, req.start_time
, cpu_time 'cpu_time_ms'
, object_name(st.objectid,st.dbid) 'ObjectName'
, ST.text
FROM sys.dm_exec_requests AS req
CROSS APPLY sys.dm_exec_sql_text(req.sql_handle) as ST
SELECT
req.session_id
, req.start_time
, cpu_time 'cpu_time_ms'
, object_name(st.objectid,st.dbid) 'ObjectName'
, substring
(REPLACE
(REPLACE
(SUBSTRING
(ST.text
, (req.statement_start_offset/2) + 1
, (
(CASE statement_end_offset
WHEN -1
THEN DATALENGTH(ST.text)
ELSE req.statement_end_offset
END
- req.statement_start_offset)/2) + 1)
, CHAR(10), ' '), CHAR(13), ' '), 1, 512) AS statement_text
FROM sys.dm_exec_requests AS req
CROSS APPLY sys.dm_exec_sql_text(req.sql_handle) as ST
Enjoy!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.