We all know that DBCC commands are a little awkward because you can't call them in a T-SQL query, and you can't relate their output to other DMV/DMF. For example, you want to return the last executed SQL statement for each user session ....
Sys.dm_exec_input_buffer
In SQL Server 2016, things are much simpler because Microsoft provides you with a new dmfsys.dm_exec_input_buffer that does the same work as DBCC INPUTBUFFER.
Using Sys.dm_exec_input_buffer is very simple: this DMF requires 2 input parameters-the session and the request ID of the specified session. The following code shows a simple example of calling a new function.
SELECT * from Sys.dm_exec_input_buffer (55, 0)
But you can do more complicated things, like using the cross apply operator to correlate information with other DMV. Let's look at the following code.
SELECT
r.session_id,
ib.event_info from
sys.dm_exec_requests R
JOIN sys.dm_exec_sessions s on s.session_id = r.session_id
CROSS APPLY sys.dm_exec_input_buffer (r.session_id, r.request_id) IB
WHERE
s.is_user_process = 1 Go
As you can see here, this query returns all the submitted SQL statements for all currently executing queries. It's simple, isn't it?