Tuesday, January 10, 2012

Instruções SQL em execução


USE

[master]
GO 
/****** Object: StoredProcedure [dbo].[sp_dba_SQL_execunow] ******/ 
SET ANSI_NULLS ON
GO 
SET QUOTED_IDENTIFIER ON
GO 
ALTER PROC [dbo].[sp_dba_SQL_execunow]
AS 
/*--------------------------------------------------------------------
Objetivo: Exibir as instruções SQL individuais que estão em execução
----------------------------------------------------------------------
---------------------------------------------------------------------*/
 

/*
Column name Data type Description
spid smallint SQL Server process ID.
ecid smallint Execution context ID used to uniquely identify the subthreads operating on behalf of a single process.
dbid smallint ID of the database currently being used by the process.
nt_username nchar(128) Windows user name for the process, if using Windows Authentication, or a trusted connection.
status nchar(30) Process ID status. For example, running and sleeping.
wait_type bigint Current wait time in milliseconds.
IndividualQuery varchar SQL Statement currently running.
Parent Query varchar Routine that contains the Individual Query.
program_name nchar(128) Name of the application program.
Hostname nchar(128) Name of the workstation.
nt_domain nchar(128) Microsoft Windows domain for the client, if using Windows Authentication, or a trusted connection.
Start_time datetime Time when the request is scheduled to run.
*/


 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


No comments: