DBCC INPUTBUFFER() is the TSQL command that we generally use to find out the submitted statement to the SQL Server Instance. Sometimes I use this command to troubleshoot the blocking scenario. If I know the session id of blocking and blocked session, then I can easily find out the TSQL statement/command passing that session id to DBCC INPUTBUFFER().
There is some limitation of this DBCC command. If I want to know the last TSQL statement for two/more sessions, then I have to execute this DBCC command with session ids, same number of times. In simple words you can say that we cannot join the output of this DBCC command with other system views or dynamic management views or functions.
In SQL Server 2016, Microsoft has provided the new dynamic management function to overcome the above issue i.e. “sys.dm_exec_input_buffer”. This dmf required two parameters – session id and request id. Let me show you the same – I am going to execute below command in a new query window. For me the session id is 57 as shown below.
Now, I’ll try to use the dmf to get the details of last tsql statement:
SELECT * FROM sys.dm_exec_input_buffer(57,0); GO
Now, we can also join the output this dmf with other dmv/dmf. Lets say, if I want to know the last TSQL statement for all the sessions, then I can get that information easily.
SELECT ss.session_id,ss.login_name,ss.status,buf.event_info FROM sys.dm_exec_sessions ss cross apply sys.dm_exec_input_buffer(ss.session_id,0) buf where ss.session_id>50;
Prince Kumar Rastogi