Exploring Input Buffer Information for Non-System Sessions in SQL Server
Ever needed to peek into what all your active, non-system SQL Server sessions are actually doing? This handy SQL query allows you to retrieve input buffer information for all user sessions within the current database, giving you insight into the commands or queries they are executing.
SQL Query to Get Session Input Buffer Details
SELECT
es.session_id,
DB_NAME(es.database_id) AS [Database Name],
es.login_time,
es.cpu_time,
es.logical_reads,
es.memory_usage,
es.[status],
ib.event_info AS [Input Buffer]
FROM
sys.dm_exec_sessions AS es WITH (NOLOCK)
CROSS APPLY
sys.dm_exec_input_buffer(es.session_id, NULL) AS ib
WHERE
es.database_id = DB_ID()
AND es.session_id > 50 -- Excludes system sessions
AND es.session_id <> @@SPID -- Excludes the current session
OPTION (RECOMPILE);
Related Commands and Resources
For quick checks, you can also use the DBCC INPUTBUFFER command:
DBCC INPUTBUFFER
For more in-depth information, check out these Microsoft documentation pages: