View the execution status during MSSQL execution

Source: Internet
Author: User
Tags what sql

Create a stored procedure: dba_WhatSQLIsExecuting

Then execute the stored procedure to view the relevant information.

The execution status during ms SQL Execution to view information such as the currently executing SQL

Which SQL statement is currently executed, and so on, which can help to progress the SQL Execution for a long time.

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 executing.

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

Parameters: None.

Revision History:

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

Example Usage:

1. exec YourServerName. master. dbo. dba_WhatSQLIsExecuting

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

BEGIN

-- Do not lock anything, and do 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> 50 -- Ignore system spids.

AND session_Id not in (@ SPID) -- Ignore this current statement.

Order by 1, 2

END

GO

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.