Problems with Sys.dm_exec_input_buffer in SQL Server 2016 _mssql

Source: Internet
Author: User

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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.