SQL Server Power Search - Get More Relevant Results

Get Running Requests

A script to get all currently running requests, with the command, resources used, and other info.

2005, 2008
select r.session_id,
    r.start_time,
    db = db_name(database_id),
    r.command,
    st.text,
    substring(st.text,
        r.statement_start_offset/2+1,
        (case when r.statement_end_offset = -1
              then len(convert(nvarchar(max), st.text)) * 2
              else r.statement_end_offset end -
         r.statement_start_offset)/2+1),
    r.blocking_session_id,
    r.wait_type,
    r.last_wait_type,
    r.cpu_time,
    r.total_elapsed_time,
    r.reads,
    r.writes,
    s.memory_usage,
    s.host_name,
    s.program_name,
    s.login_name
from sys.dm_exec_requests r
join sys.dm_exec_connections c on r.session_id = c.session_id 
join sys.dm_exec_sessions s on s.session_id = r.session_id 
cross apply sys.dm_exec_sql_text(r.sql_handle) st 
where r.session_id > 50
order by r.session_id

2000
The SQL Server 2000 version doesn't return as much information. It is possible to get the command being run using DBCC INPUTBUFFER (limited to 255 characters) or ::fn_get_sql.

select spid, last_batch, db_name(dbid), cmd, blocked, lastwaittype, cpu,
physical_io, memusage, hostname, program_name, loginame
from master..sysprocesses
where spid > 50
and cmd <> 'AWAITING COMMAND'


 
Not found what you're looking for?
Use this search box. It is tuned for SQL Server searches. Try it and see!



Do you have a question about SQL Server? Would you like to answer a question?

Go to the SQL FAQ to get started.