Question: Is there any way to replace DBCC INPUTBUFFER Command?
Answer: In a recent interview, I heard this question and it caught me off guard. Honestly, when I heard this question being asked to interview candidates. Honestly the candidate did not know the answer and I had decided that I will write about it when I return home. When I was back home, I noticed there is SQL Server 2016 SP1 released.
I have always seen that new releases bring new functionalities and that is an awesome opportunity for us to learn something new. Having said that, when SQL Server 2016 SP1 got released, I personally felt this was no ordinary release. There were several interesting features and enhancements (though small) were getting added. Many look trivial and sometimes makes us think why this was not there before? Why didn’t Microsoft not add these capabilities into the product? Those questions are beyond the scope of this blog. I am happy and glad that this is happening. The new Microsoft is agile and has a fresh perspective. One such big thing is “SQL on Linux”. I do want to play and blog about it, this discussion is for some other time.
Coming back to core content for this blog. When things go wrong in SQL Server, one of the things that I have seen DBA’s do is to query the sp_who, sp_locks etc and find which connection is waiting or blocking state. Then given the connection number, they also use the DBCC INPUTBUFFER command to find what statements are running for that session. I have always found this two-step process less convoluted.
Welcome SQL Server 2016 SP1, we now have an interesting DMV added (dm_exec_input_buffer). The usage of it would look like:
SELECT es.session_id, ib.event_info FROM sys.dm_exec_sessions AS es CROSS APPLY sys.dm_exec_input_buffer(es.session_id, NULL) AS ib WHERE es.session_id > 50 GO
The output looks like this:
This command as you see is functionally equivalent to DBCC INPUTBUFFER. Having said that there are advantages over DBCC INPUTBUFFER tough. Some to mention, this command directly returns a rowset, it can be conveniently used with sys.dm_exec_sessions or sys.dm_exec_requests by doing a CROSS APPLY as shown in the example above and finally, it can use a simple query to retrieve input buffer of multiple sessions without the need for a script and temp table.
Do let me know how useful this would be for you. Will you be using this DMV in the future? What innovative ways will you use them? Let me know via comments below.
Reference: Pinal Dave (https://blog.sqlauthority.com)