To view the SQL that is being executed

Source: Internet
Author: User
Tags what sql

Brief introduction
It is known that the SP_WHO2 command is used to view the SPIDs of the currently executing SQL statement. But the display
Information is limited. For example, it only shows execution select,delete,update, and so on. The actual SQL command is not visible.

Know which SQL statement is being executed for our debug program, find out which statements take a long time, or which statements produce
Clogging is very important. And this is often used to see which step the stored procedure executes. To which statement to execute.

So we need a better command than Sp_who2 to do the work.
This command uses the dynamic management view of SQL Management Views (DMVs). So just do it in SQL2005 or
On a higher version.

Return To field Introduction:

Column Name Type Describe
spid smallint SQL Server process ID.
Ecid smallint Econtext ID
dbid smallint Database ID
Nt_username NCHAR (128) User name
Status NCHAR (30) State
Wait_type bigint The current waiting milliseconds.
Individual Query varchar The currently executing SQL statement
Parent Query varchar Upper-level SQL statements
Program_name NCHAR (128) Program Name
Hostname NCHAR (128) Host Name
Nt_domain NCHAR (128) Domain name
Start_time Datetime Start time

CREATE PROC [dbo]. [Dba_whatsqlisexecuting]
As
/*--------------------------------------------------------------------
Displays the currently executing SQL statement

----------------------------------------------------------------------
How to use:
1. Exec YourServerName.master.dbo.dba_WhatSQLIsExecuting
---------------------------------------------------------------------*/
BEGIN
--Does not lock anything, and does not get held to any locks.
SET TRANSACTION Isolation Level READ UNCOMMITTED
-What SQL statements is currently Running?
SELECT [Spid] = session_id
, ecid
, [Database] = db_name (sp.dbid)
, [User] = Nt_username
, [Status] = Er.status
, [Wait] = Wait_type
, [individual Query] = SUBSTRING (Qt.text,
ER.STATEMENT_START_OFFSET/2,
(case when er.statement_end_offset =-1
Then LEN (CONVERT (NVARCHAR (MAX), Qt.text)) * 2
ELSE Er.statement_end_offset End-
Er.statement_start_offset)/2)
, [Parent Query] = Qt.text
, program = Program_name
, Hostname
, Nt_domain
, start_time
From sys.dm_exec_requests ER
INNER JOIN sys.sysprocesses sp on er.session_id = Sp.spid
Cross APPLY Sys.dm_exec_sql_text (er.sql_handle) as Qt
WHERE session_id >-Ignore system SPIDs.
and session_id not in (@ @SPID)--Ignore the current statement.
ORDER by 1, 2
END

To view the SQL that is being executed

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.