View t-sql SPID is executing

585 pts.
Tags:
SQL
SQL Server 2005
Is there a table that I can query to view what sql statements a spid is running? I do not want to use the dbcc inputbuffer.

Software/Hardware used:
SQL SERVER 2005
ASKED: January 12, 2010  4:20 PM
UPDATED: January 13, 2010  2:10 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

Get the sql_handle from sys.sysprocesses for that spid.

Then use the sys.dm_exec_sql_text system function to get the batch of code which was executed against the engine. You can use the stmt_start and smtm_end values from sys.sysprocesses to figure out the specific statement within the batch which is being executed.

<pre>DECLARE @sql_handle varbinary(64)
DECLARE @stmt_start int
DECLARE @stmt_end int

DECLARE @spid_i INT = 55 /*Your Spid to check*/

SELECT @sql_handle = sql_handle,
@stmt_start = stmt_start,
@stmt_end = stmt_end
from sys.sysprocesses with (nolock)
where spid = @spid_i

select text as ‘Full Query’,
case when @stmt_start > 0 then
case when @stmt_end > @stmt_start then
substring(text, @stmt_start/2, (@stmt_end/2)-(@stmt_start/2))
else
substring(text, @stmt_start/2, len(text))
end
else
null
end as ‘Current Command’, @stmt_start, @stmt_end
from sys.dm_exec_sql_text(@sql_handle)</pre>

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following