-
Notifications
You must be signed in to change notification settings - Fork 0
/
vProcesses.sql
98 lines (94 loc) · 2.26 KB
/
vProcesses.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
SQL Stats query/view
*/
USE master
GO
IF EXISTS (SELECT NULL FROM sys.views WHERE NAME ='vProcesses')
DROP VIEW dbo.vProcesses
GO
DECLARE @SQL NVARCHAR(MAX)
IF (select SUBSTRING(CONVERT(VARCHAR(20),SERVERPROPERTY('ProductVersion')),0,CHARINDEX('.',CONVERT(VARCHAR(20),SERVERPROPERTY('ProductVersion'),0)))) >= 11
BEGIN
PRINT 'Creating view for SQL 2012 and up.'
/* for SQL 2012 and up
Earlier versions of SQL server doesn't have database_id in sys.dm_exec_sessions
*/
SET @SQL = '
CREATE VIEW dbo.vProcesses
AS
SELECT
s.session_id,
r.request_id,
s.login_time,
s.[host_name],
s.[program_name],
s.client_interface_name,
s.login_name,
r.status,
r.percent_complete,
r.blocking_session_id,
r.wait_type,
r.last_wait_type,
r.command,
s.database_id,
sd.name,
s.cpu_time,
s.memory_usage,
r.sql_handle,
qs.execution_count,
qs.last_execution_time,
qs.last_worker_time,
qs.last_physical_reads,
qs.last_logical_reads,
qs.last_logical_writes,
qs.last_elapsed_time,
r.plan_handle,
t.text AS SQLText,
p.query_plan AS QueryPlanXML
FROM sys.dm_exec_sessions AS s
LEFT JOIN sys.databases AS sd
ON sd.database_id = s.database_id
LEFT JOIN sys.dm_exec_requests AS r
ON r.session_id = s.session_id
LEFT JOIN sys.dm_exec_query_stats AS qs
ON qs.plan_handle = r.plan_handle
OUTER APPLY sys.dm_exec_sql_text(r.sql_handle) AS t
OUTER APPLY sys.dm_exec_query_plan(r.plan_handle) AS p'
END
ELSE
BEGIN
PRINT 'Creating view for SQL 2008 R2, or older.'
/* For older SQL versions (2008R2 and older) */
SET @SQL = '
CREATE VIEW dbo.vProcesses
AS
SELECT
s.session_id,
r.request_id,
s.login_time,
s.[host_name],
s.[program_name],
s.client_interface_name,
s.login_name,
r.status,
r.percent_complete,
r.blocking_session_id,
r.wait_type,
r.last_wait_type,
r.command,
r.database_id,
sd.name,
s.cpu_time,
s.memory_usage,
r.sql_handle,
t.text AS SQLText,
p.query_plan AS QueryPlanXML
FROM sys.dm_exec_sessions AS s
LEFT JOIN sys.dm_exec_requests AS r
ON r.session_id = s.session_id
LEFT JOIN sys.databases AS sd
ON sd.database_id = r.database_id
OUTER APPLY sys.dm_exec_sql_text(r.sql_handle) AS t
OUTER APPLY sys.dm_exec_query_plan(r.plan_handle) AS p'
END
EXEC sp_executeSQL @SQL