View execution status during MSSQL execution

Source: Internet
Author: User
Tags mssql what sql

To create a stored procedure: dba_whatsqlisexecuting

Then execute the stored procedure to see the relevant information.

Execution status during MS SQL execution to view information such as SQL that is currently executing

Which SQL is currently executing, and so on, this can help a long time SQL execution make progress bar.

Use [RMA_DWH]

Go

/****** object:storedprocedure [dbo]. [Dba_whatsqlisexecuting] Script date:07/12/2013 10:28:27 ******/

SET ANSI_NULLS on

Go

SET QUOTED_IDENTIFIER ON

Go

CREATE PROC [dbo]. [Dba_whatsqlisexecuting]

As

/*--------------------------------------------------------------------

Purpose:shows What individual SQL statements are currently.

----------------------------------------------------------------------

Parameters:none.

Revision History:

24/07/2008 Ian_Stirk@yahoo.com Initial Version

Example Usage:

1. Exec YourServerName.master.dbo.dba_WhatSQLIsExecuting

---------------------------------------------------------------------*/

BEGIN

--Does not lock anything, and does not get held up by any locks.

SET TRANSACTION Isolation Level READ UNCOMMITTED

--What SQL statements Are 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 This current statement.

Order by 1, 2

End

Go

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.