@@ -2,7 +2,8 @@ USE tempdb;
2
2
3
3
DECLARE @s VARCHAR (MAX ), @t VARCHAR (MAX );
4
4
DECLARE @tableName varchar (255 );
5
- DECLARE @DeleteFlag bit = 0 ; /* 0 = drop, 1 = truncate, 2 = No Action */
5
+ DECLARE @DeleteFlag tinyint ; /* 0 = drop, 1 = truncate, 2 = No Action */
6
+ SET @DeleteFlag = 2 ;
6
7
SET @tableName = ' tempdb.dbo.WhoIsActive_ResultSet' ;
7
8
8
9
EXEC sp_WhoIsActive @sort_order = ' [start_time] ASC' , @get_outer_command= 1 , @find_block_leaders= 1 -- ,@get_full_inner_text=1
@@ -13,14 +14,15 @@ EXEC sp_WhoIsActive @sort_order = '[start_time] ASC', @get_outer_command=1, @fin
13
14
14
15
SET @s = REPLACE (@s, ' <table_name>' , @tableName)
15
16
16
- IF ( @DeleteFlag = 0 )
17
+ IF @DeleteFlag = 0
17
18
SET @t = ' IF OBJECT_ID('' '+ @tableName+ ' '' ) IS NOT NULL DROP TABLE '+ @tableName;
18
- IF ( @DeleteFlag = 1 )
19
+ IF @DeleteFlag = 1
19
20
SET @t = ' IF EXISTS(SELECT * FROM '+ @tableName+ ' ) TRUNCATE TABLE '+ @tableName;
20
- IF ( @DeleteFlag = 2 )
21
+ IF @DeleteFlag = 2
21
22
SET @t = ' -- ignore' ;
22
23
23
24
EXEC (@t);
25
+ PRINT @t;
24
26
SET @s = ' IF OBJECT_ID('' '+ @tableName+ ' '' ) IS NULL '+ @s;
25
27
EXEC (@s);
26
28
@@ -31,8 +33,7 @@ EXEC sp_WhoIsActive @sort_order = '[start_time] ASC', @get_outer_command=1, @fin
31
33
-- ,@get_plans=1 /* 1 = current query, 2 = entire batch */
32
34
,@destination_table = @tableName;
33
35
34
- -- select r.*
35
- -- from dbo.WhoIsActive_ResultSet as r; -- @tableName
36
+ select distinct [collection_time] from dbo .WhoIsActive_ResultSet as r; -- @tableName
36
37
-- go
37
38
38
39
@@ -66,15 +67,29 @@ EXEC sp_WhoIsActive @sort_order = '[start_time] ASC', @get_outer_command=1, @fin
66
67
)
67
68
-- select * from T_BLOCKERS
68
69
69
- SELECT [BLOCKING_TREE] = N ' ' + REPLICATE (N ' | ' , LEN (LEVEL )/ 4 - 1 )
70
+ SELECT r.[collection_time],
71
+ [BLOCKING_TREE] = N ' ' + REPLICATE (N ' | ' , LEN (LEVEL )/ 4 - 1 )
70
72
+ CASE WHEN (LEN (LEVEL )/ 4 - 1 ) = 0
71
73
THEN ' HEAD - '
72
74
ELSE ' |------ '
73
75
END
74
76
+ CAST (r .session_id AS NVARCHAR (10 )) + N ' ' + (CASE WHEN LEFT (r.[batch_text],1 ) = ' (' THEN SUBSTRING (r.[batch_text],CHARINDEX (' exec' ,r.[batch_text]),LEN (r.[batch_text])) ELSE r.[batch_text] END ),
75
- r.[dd hh:mm:ss.mss], r.[session_id], r.[sql_text], r.[sql_command], r.[login_name], r.[wait_info], r.[tempdb_allocations], r.[tempdb_current],
76
- r.[blocking_session_id], r.[blocked_session_count], r.[reads], r.[writes], r.[physical_reads], r.[CPU], r.[used_memory], r.[status], r.[open_tran_count],
77
- r.[percent_complete], r.[host_name], r.[database_name], r.[program_name], r.[start_time], r.[login_time], r.[request_id], r.[collection_time]
77
+ r.[dd hh:mm:ss.mss], r.[wait_info], r.[blocked_session_count], r.[blocking_session_id], -- r.[sql_text], r.[sql_command],
78
+ r.[login_name], r.[host_name], r.[database_name], r.[program_name], r.[tempdb_allocations], r.[tempdb_current],
79
+ r.[reads], r.[writes], r.[physical_reads], r.[CPU], r.[used_memory], r.[status], r.[open_tran_count],
80
+ r.[percent_complete], r.[start_time], r.[login_time], r.[request_id]
78
81
FROM T_BLOCKERS AS r
79
82
ORDER BY collection_time, LEVEL ASC ;
80
83
84
+ select @@servername as srvName, r .login_name , r .program_name , r .database_name , count (r .session_id ) as session_counts
85
+ from tempdb .dbo .WhoIsActive_ResultSet AS r
86
+ group by r .login_name , r .program_name , r .database_name
87
+ having count (r .session_id ) > (select count (distinct [collection_time]) from dbo .WhoIsActive_ResultSet as r)
88
+ order by session_counts desc
89
+ go
90
+
91
+ /*
92
+ delete from tempdb.dbo.WhoIsActive_ResultSet
93
+ where collection_time not in ('2020年07月10日 15:52:58.490')
94
+ */
95
+
0 commit comments