Friday, 30 September 2016

Get the currently executing sql query

With the help of below sql script we will get the currently sql query executing on the database and we can get how long time the sql query have been taken.
SELECT      r.start_time [Start Time]
              ,DateDiff(ss,r.start_time,GetDate()) as [execution time in seconds]
            ,session_ID [SPID],
            DB_NAME(database_id) [Database],
            SUBSTRING(t.text,(r.statement_start_offset/2)+1,
            CASE WHEN statement_end_offset=-1 OR statement_end_offset=0
            THEN (DATALENGTH(t.Text)-r.statement_start_offset/2)+1
            ELSE (r.statement_end_offset-r.statement_start_offset)/2+1
            END) [Executing SQL],
            Status,command,wait_type,wait_time,wait_resource,
            last_wait_type
FROM        sys.dm_exec_requests r
OUTER APPLY sys.dm_exec_sql_text(sql_handle) t
WHERE       session_id != @@SPID -- don't show this query
AND         session_id > 50 -- don't show system queries
ORDER BY    r.start_time


  



No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts