(DIR) Return Create A Forum - Home
       ---------------------------------------------------------
       ExcelSoft Database Professionals
 (HTM) https://esdbp.createaforum.com
       ---------------------------------------------------------
       *****************************************************
 (DIR) Return to: Scripts
       *****************************************************
       #Post#: 21--------------------------------------------------
       Find currently running statements for an SPID
       By: Yasub_exceldbp Date: March 10, 2014, 5:48 am
       ---------------------------------------------------------
       Hello!!!
       If you have been a DB Developer for several years, then you
       would have mostly run some large scripts. Large in size and
       large in terms of the data they effect.
       This is usually a pretty unnerving task, as you don't know what
       statement of the Script is currently running.
       The below query might help you to keep your cool a bit. :)
       ----------------------------------------------------------------
       -----------------------
       --Replace the relevant SPID that you wish to track in the filter
       condition.
       SELECT [Spid] = session_Id
       , ecid
       , [Database] = DB_NAME(sp.dbid)
       , [User] = nt_username
       , [Status] = er.status
       , [Wait] = wait_type
       , [Individual Query] = SUBSTRING (qt.text,
       er.statement_start_offset/2,
       (CASE WHEN er.statement_end_offset = -1
       THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2
       ELSE er.statement_end_offset END -
       er.statement_start_offset)/2)
       ,[Parent Query] = qt.text
       , Program = program_name
       , Hostname
       , nt_domain
       , start_time
       FROM sys.dm_exec_requests er
       INNER JOIN sys.sysprocesses sp ON er.session_id = sp.spid
       CROSS APPLY sys.dm_exec_sql_text(er.sql_handle)as qt
       WHERE session_Id = 111-- <Place the SPID that you want to track,
       here.>
       ORDER BY 1, 2
       ----------------------------------------------------------------
       -----------------------
       Hope this has been useful.
       Please post your comments, if you have any suggestions or in
       case you have better and more efficient methods that you have
       been using.
       Thanks!
       -Yasub :)
       *****************************************************