Sys. dm_exec_input_buffer in SQL Server 2016, sys. dmexecrequests

Source: Internet
Author: User

Sys. dm_exec_input_buffer in SQL Server 2016, sys. dmexecrequests

We all know that DBCC commands are a bit embarrassing, because you cannot call them in T-SQL queries, and you cannot associate 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 become much simpler, because Microsoft provides you with a new DMFsys. dm_exec_input_buffer, which does the same work as DBCC INPUTBUFFER.

Using sys. dm_exec_input_buffer is very simple: This DMF requires two 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) GO

But you can do more complex things, such as associating information with other DMV using the cross apply operator. Let's take a look at the following code.

SELECTr. session_id, ib. event_infoFROM sys. dm_exec_requests rJOIN sys. dm_exec_sessions s ON s. session_id = r. session_idCROSS APPLY sys. dm_exec_input_buffer (r. session_id, r. request_id) ibWHEREs. is_user_process = 1GO

As you can see here, this query returns all submitted SQL statements for all currently executed queries. It's easy, 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.