Currently running query inside a stored procedure

15,204

Solution 1

There is an excellent stored procedure to get extended information on currently running queries. It's available to download from: http://whoisactive.com

Solution 2

SELECT SUBSTRING(st.text, ( r.statement_start_offset / 2 ) + 1, 
              ( ( CASE WHEN r.statement_end_offset <= 0
                       THEN DATALENGTH(st.text) 
              ELSE r.statement_end_offset END - 
       r.statement_start_offset ) / 2 ) + 1) AS statement_text 
FROM   sys.dm_exec_requests r 
       CROSS APPLY sys.dm_exec_sql_text(sql_handle) st 
WHERE  session_id = 65 

Solution 3

Use SQL Profiler; as the name suggests, it's the main profiling tool for SQL Server and it can show the execution time for each statement inside a procedure.

Share:
15,204
cederlof
Author by

cederlof

Doing his best to be a pragmatic programmer and has an interest towards .NET Framework/Core, JavaScript and the like.

Updated on June 12, 2022

Comments

  • cederlof
    cederlof about 2 years

    I have a stored procedure that is currently running, and seems to hang/lock on a specific query. How can i see which query? Preferably without modifying the proc.

    Using

    DBCC Inputbuffer (65)
    

    gives me

    Language Event 0 EXEC mySP;

  • cederlof
    cederlof over 12 years
    That was my thought as well, but all it shows me is the "EXEC mySP" query used to trigger the SP. Or am I using incorrect filtering?
  • cederlof
    cederlof over 12 years
    Cool, that seems to work great! (For other people wanting this, change/remove the WHERE-line.)
  • Martin Smith
    Martin Smith over 12 years
    @cederlof - You would need to capture the SP:StmtCompleted and SP:StmtStarting events in Profiler.
  • Gabriel McAdams
    Gabriel McAdams over 10 years
    Its helpful to add a line WHERE session_id != < this session id >
  • Martin Smith
    Martin Smith over 10 years
    @GabrielMcAdams - In the case of the question they are specifically interested in session id 65 (from DBCC Inputbuffer (65))
  • Gabriel McAdams
    Gabriel McAdams over 10 years
    @MartinSmith - You're right, and I didn't mean that it would be helpful to the original poster - but to add to his comment here that mentions removing the where clause.