SQL Server 2016 – sys.dm_exec_input_buffer

Hi Friends,

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.

SQL Server 2016 – sys.dm_exec_input_buffer

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

SQL Server 2016 – sys.dm_exec_input_buffer 2

   

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;

SQL Server 2016 – sys.dm_exec_input_buffer 3

HAPPY LEARNING!

Regards:

Prince Kumar Rastogi

Like us on FaceBook | Join the fastest growing SQL Server group on FaceBook

Follow Prince Rastogi on Twitter | Follow Prince Rastogi on FaceBook

   

About Prince Rastogi

Prince Rastogi is working as Database Administrator at Elephant Insurance, Richmond. He is having more than 8 years of experience and worked in ERP Domain, Wealth Management Domain. Currently he is working in Insurance domain. In the starting of his career he was working on SQL Server, Internet Information Server and Visual Source Safe. He is post graduate in Computer Science. Prince is ITIL certified professional. Prince likes to explore technical things for Database World and Writing Blogs. He is Technical Editor and Blogger at SQLServerGeeks.com. He is a regular speaker at DataPlatformDay events in Delhi NCR. He has also presented some in depth sessions about SQL Server in SQL Server Conferences in Bangalore.

View all posts by Prince Rastogi →

Leave a Reply

Your email address will not be published.