1: DECLARE @html NVARCHAR(MAX);
2:
3: with tb
4: as
5: (
6: SELECT c.session_id,t.text
7: FROM sys.dm_exec_connections c
8: CROSS APPLY sys.dm_exec_sql_text (c.most_recent_sql_handle) t
9: )
10: select distinct x.spid,DB_NAME(x.dbid) as dbname,x.last_batch,x.
hostname,x.program_name,x.nt_domain,x.nt_username,tb.text
11: into #T
12: from sys.sysprocesses x with (nolock)
13: inner join tb on x.spid=tb.session_id
14: where x.last_batch
15: and x.program_name<>'Report Server' >'Report Server'
16: and nt_domain<>'NT AUTHORITY'
17: and x.status<>'sleeping'
18: and x.hostname<>'HZCSRPTSRV'
19:
20: if exists(select top 1 * from #T)
21: begin
22: SET @html = '' ;
23:
24: SET @html = @html + CAST(( SELECT 3 [@cellpadding],0 [@cellspacing],
'font-family:verdana;font-size:10px;' [@style],1 [@border],
25: ( SELECT [@class] = 'header', 'spid' [text()] FOR XML PATH('th'), TYPE) tr,
26: ( SELECT [@class] = 'header', 'dbname' [text()] FOR XML PATH('th'), TYPE) tr,
27: ( SELECT [@class] = 'header', 'last_batch' [text()] FOR XML PATH('th'), TYPE) tr,
28: ( SELECT [@class] = 'header', 'hostname' [text()] FOR XML PATH('th'), TYPE) tr,
29: ( SELECT [@class] = 'header', 'program_name' [text()] FOR XML PATH('th'), TYPE) tr,
30: ( SELECT [@class] = 'header', 'nt_domain' [text()] FOR XML PATH('th'), TYPE) tr,
31: ( SELECT [@class] = 'header', 'nt_username' [text()] FOR XML PATH('th'), TYPE) tr,
32: ( SELECT [@class] = 'header', 'text' [text()] FOR XML PATH('th'), TYPE) tr,
33: ( SELECT
34: ( SELECT [@class] = 'cell_text', spid [text()] FOR XML PATH('td'), TYPE ),
35: ( SELECT [@class] = 'cell_text', dbname [text()] FOR XML PATH('td'), TYPE ),
36: ( SELECT [@class] = 'cell_text', last_batch [text()] FOR XML PATH('td'), TYPE ),
37: ( SELECT [@class] = 'cell_text', hostname [text()] FOR XML PATH('td'), TYPE ),
38: ( SELECT [@class] = 'cell_text', program_name [text()] FOR XML PATH('td'), TYPE ),
39: ( SELECT [@class] = 'cell_text', nt_domain [text()] FOR XML PATH('td'), TYPE ),
40: ( SELECT [@class] = 'cell_text', nt_username [text()] FOR XML PATH('td'), TYPE ),
41: ( SELECT [@class] = 'cell_text', text [text()] FOR XML PATH('td'), TYPE )
42: FROM (
43: select spid,dbname,last_batch,hostname,program_name,nt_domain,nt_username,text
44: from #T
45: ) data
46: FOR XML PATH('tr'), TYPE
47: )
48: FOR XML PATH('table'), TYPE
49: ) AS VARCHAR(MAX));
50: drop table #T;
51: --Send Email
52: EXEC msdb.dbo.sp_send_dbmail
53: @profile_name = 'DBMAIL'
54: ,@recipients = 'DarrenXie@QQ.com'
55: ,@copy_recipients = 'QQQQQ@QQ.com'
56: ,@subject = 'Camstar HZCSRPTSRV long runtime process'
57: ,@body = @html
58: ,@importance ='High'
59: ,@body_format= 'HTML'
60: end
61: else
62: begin
63: drop table #T;
64: end
|