forked from Thomas-S-B/SQLServerTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailJobTimeLine.sql
683 lines (562 loc) · 26.8 KB
/
MailJobTimeLine.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
--###################################################################################################
-- This SQL sends an email with all Jobs as an graphical timeline
--
-- You can configure this in the section "Configuration"
--
-- History:
-- 2017-07-28 Initial version
--###################################################################################################
SET NOCOUNT ON
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
DECLARE @MinimalJobDurationInSeconds INT
DECLARE @EmailTo VARCHAR(500)
DECLARE @DaysPast INT
DECLARE @HoursPast INT
DECLARE @Servername VARCHAR(50)
--###################################################################################################
--
-- Configuration Start
--
--###################################################################################################
--Wer soll alles eine Mail bekomen?
SET @EmailTo = '[email protected];[email protected]'
--How many days in the past should be displayed?
SET @DaysPast = 1
--How many hours in the past should be displayed?
SET @HoursPast = 0
--How long must a job taken to displayed?
SET @MinimalJobDurationInSeconds = 0
--###################################################################################################
--
-- Configuration End
--
--###################################################################################################
--###################################################################################################
--
-- Calculate timespan
--
--###################################################################################################
SET @StartDate = DateAdd(hh, -(@HoursPast), GETDATE() - @DaysPast)
SET @EndDate = GETDATE()
--###################################################################################################
--
-- Textliterals
--
--###################################################################################################
DECLARE @TEXT_SUCCESS VARCHAR(20)
SET @TEXT_SUCCESS = 'Successful'
--###################################################################################################
--
-- Colors
--
--###################################################################################################
DECLARE @COLOR_ERROR VARCHAR(10)
SET @COLOR_ERROR = '#FF4136'
DECLARE @COLOR_SUCCESS VARCHAR(10)
SET @COLOR_SUCCESS = '#2ECC40'
DECLARE @COLOR_RETRY VARCHAR(10)
SET @COLOR_RETRY = '#FFDC00'
DECLARE @COLOR_ABORTED VARCHAR(10)
SET @COLOR_ABORTED = '#AAAAAA'
DECLARE @COLOR_UNDEFINED VARCHAR(10)
SET @COLOR_UNDEFINED = '#111111'
DECLARE @COLOR_RUNNING VARCHAR(10)
SET @COLOR_RUNNING = '#7FDBFF'
DECLARE @COLOR_SERVERSTART VARCHAR(10)
SET @COLOR_SERVERSTART = '#FF851B'
--###################################################################################################
--
-- Cleanup, perhaps there are temptables from other executions
--
--###################################################################################################
IF OBJECT_ID('tempdb..#JobExecutionTimes') IS NOT NULL
DROP TABLE #JobExecutionTimes;
IF OBJECT_ID('tempdb..##TimelineGraph') IS NOT NULL
DROP TABLE ##TimelineGraph;
--###################################################################################################
--
-- Create table which holds the generated HTML
--
--###################################################################################################
CREATE TABLE ##TimelineGraph
(ID INT IDENTITY(1, 1)
NOT NULL
,HTML VARCHAR(8000) NULL --8000, so it works with SQL-Server < 2008R2
)
--###################################################################################################
--
-- Create table of the jobs
--
--###################################################################################################
SELECT JOBDATA.*
INTO #JobExecutionTimes
FROM (
--Jobs, which are currently not running
SELECT JOB.name AS JobName
,CAT.name AS CatName
,CONVERT(DATETIME, CONVERT(CHAR(8), HIS.run_date, 112) + ' '
+ STUFF(STUFF(RIGHT('000000'
+ CONVERT(VARCHAR(8), HIS.run_time), 6),
5, 0, ':'), 3, 0, ':'), 120) AS SDT
,DATEADD(s,
((HIS.run_duration / 10000) % 100 * 3600)
+ ((HIS.run_duration / 100) % 100 * 60)
+ HIS.run_duration % 100,
CONVERT(DATETIME, CONVERT(CHAR(8), HIS.run_date, 112)
+ ' ' + STUFF(STUFF(RIGHT('000000'
+ CONVERT(VARCHAR(8), HIS.run_time),
6), 5, 0, ':'), 3, 0, ':'), 120)) AS EDT
,JOB.description
,HIS.run_status
,CASE WHEN HIS.run_status = 0 THEN @COLOR_ERROR
WHEN HIS.run_status = 1 THEN @COLOR_SUCCESS
WHEN HIS.run_status = 2 THEN @COLOR_RETRY
WHEN HIS.run_status = 3 THEN @COLOR_ABORTED
ELSE @COLOR_UNDEFINED
END AS JobStatus
,CASE WHEN HIS.run_status = 0 THEN HIS.message -- 0 = Error (red)
WHEN HIS.run_status = 1 THEN @TEXT_SUCCESS -- 1 = Successful (green)
WHEN HIS.run_status = 2 THEN HIS.message -- 2 = New try (yellow)
WHEN HIS.run_status = 3 THEN HIS.message -- 3 = Aborted (gray)
ELSE HIS.message -- undefined status (black)
END AS JobMessage
FROM msdb.dbo.sysjobs AS JOB
LEFT JOIN msdb.dbo.sysjobhistory AS HIS ON HIS.job_id = job.job_id
INNER JOIN msdb.dbo.syscategories AS CAT ON CAT.category_id = JOB.category_id
WHERE CONVERT(DATETIME, CONVERT(CHAR(8), HIS.run_date, 112) + ' '
+ STUFF(STUFF(RIGHT('000000'
+ CONVERT(VARCHAR(8), HIS.run_time), 6),
5, 0, ':'), 3, 0, ':'), 120) BETWEEN @StartDate
AND
@EndDate
AND HIS.step_id = 0 -- step_id = 0 is the job, step_id > 0 are the subjobs of this job
AND ((HIS.run_duration / 10000) % 100 * 3600)
+ ((HIS.run_duration / 100) % 100 * 60) + HIS.run_duration
% 100 >= @MinimalJobDurationInSeconds
UNION ALL
--Jobs currently running
SELECT JOB.name AS JobName
,CAT.name AS CatName
,JA.start_execution_date AS SDT
,GETDATE() AS EDT
,JOB.description
,HIS.run_status
,CASE WHEN HIS.run_status = 0 THEN @COLOR_ERROR
WHEN HIS.run_status = 1 THEN @COLOR_SUCCESS
WHEN HIS.run_status = 2 THEN @COLOR_RETRY
WHEN HIS.run_status = 3 THEN @COLOR_ABORTED
WHEN HIS.run_status IS NULL THEN @COLOR_RUNNING
ELSE @COLOR_UNDEFINED
END AS JobStatus
,CASE WHEN HIS.run_status = 0 THEN HIS.message -- 0 = Error (red)
WHEN HIS.run_status = 1 THEN @TEXT_SUCCESS -- 1 = Successful (green)
WHEN HIS.run_status = 2 THEN HIS.message -- 2 = New try (yellow)
WHEN HIS.run_status = 3 THEN HIS.message -- 3 = Aborted (gray)
WHEN HIS.run_status IS NULL THEN 'Running currently'
ELSE HIS.message -- undefined status (black)
END AS JobMessage
FROM msdb.dbo.sysjobactivity AS JA
LEFT JOIN msdb.dbo.sysjobhistory AS HIS ON HIS.instance_id = JA.job_history_id
JOIN msdb.dbo.sysjobs AS JOB ON JOB.job_id = JA.job_id
JOIN msdb.dbo.sysjobsteps AS JS ON JS.job_id = JA.job_id
AND ISNULL(JA.last_executed_step_id,
0) + 1 = JS.step_id
LEFT JOIN msdb.dbo.syscategories AS CAT ON CAT.category_id = JOB.category_id
WHERE JA.session_id = (SELECT TOP 1
session_id
FROM msdb.dbo.syssessions
ORDER BY agent_start_date DESC
)
AND JA.start_execution_date IS NOT NULL
AND JA.stop_execution_date IS NULL
) AS JOBDATA
ORDER BY JOBDATA.JobName
IF NOT EXISTS ( SELECT 1
FROM #JobExecutionTimes )
GOTO NothingToDo
--###################################################################################################
--
-- Create errormessage
--
--###################################################################################################
DECLARE @ERROR_COUNT AS INTEGER
DECLARE @ERROR_TEXT AS VARCHAR(50)
SET @ERROR_COUNT = (SELECT COUNT(*) FROM #JobExecutionTimes WHERE run_status=0)
IF @ERROR_COUNT > 0
SET @ERROR_TEXT = 'There are ' + CONVERT(varchar(4), @ERROR_COUNT) +' faulty jobs.'
ELSE
SET @ERROR_TEXT = ''
--###################################################################################################
--
-- Html Timeline - Head
--
--###################################################################################################
INSERT INTO ##TimelineGraph
(HTML
)
SELECT '<html>
<head>
<style>
.google-visualization-tooltip {
width: 400px !important;
height: 200px !important;
border-radius: 8px !important;
border: 2px solid rgb(1, 1, 1) !important;
background-color: rgb(50, 50, 50) !important;
color: rgb(230, 230, 230) !important;
font-size: 14px !important;
font-family: Helvetica !important;
box-shadow: 7px 5px 7px 0px rgba(50, 50, 50, 0.75) !important;
padding: 6px 6px 6px 6px !important;
opacity: 0.85 !important; }
'
INSERT INTO ##TimelineGraph
(HTML
)
SELECT '
#legend ul, li {
margin: 0px;
list-style: none;
display: inline-block;
font-size: 14px;
font-family: Helvetica;
padding: 0px 0px 5px 0px;
}
.rectangleBasis,
.legendSuccess,
.legendRunning,
.legendError,
.legendRetry,
.legendAborted,
.legendServerstart,
.legendUndefined {
float: left;
width: 30px;
height: 15px;
margin: 0px 3px 0px 15px;
}
.legendSuccess {
background: '+ @COLOR_SUCCESS + ';
}
.legendRunning {
background: '+ @COLOR_RUNNING + ';
}
.legendError {
background: '+ @COLOR_ERROR + ';
}
.legendRetry {
background: '+ @COLOR_RETRY + ';
}
.legendAborted {
background: '+ @COLOR_ABORTED + ';
}
.legendServerstart {
background: '+ @COLOR_SERVERSTART + ';
}
.legendUndefined {
background: '+ @COLOR_UNDEFINED + ';
}
'
INSERT INTO ##TimelineGraph
(HTML)
SELECT '</style>'
INSERT INTO ##TimelineGraph
(HTML
)
SELECT '<!--<META HTTP-EQUIV="refresh" CONTENT="3">-->
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={''modules'':[{''name'':''visualization'', ''version'':''1'',''packages'':[''timeline'']}]}"></script>'
INSERT INTO ##TimelineGraph
(HTML
)
SELECT ' <script type="text/javascript">
google.setOnLoadCallback(drawChart);
function drawChart() {'
INSERT INTO ##TimelineGraph
(HTML
)
SELECT ' var container = document.getElementById(''JobTimeline'');
var chart = new google.visualization.Timeline(container);
var dataTable = google.visualization.arrayToDataTable(['
INSERT INTO ##TimelineGraph
(HTML
)
SELECT '[''Category'', ''Name'', {role: ''style''}, ''Start'', ''End''],'
--###################################################################################################
--
-- Html Timeline - Jobdata
--
--###################################################################################################
INSERT INTO ##TimelineGraph
(HTML
)
SELECT ' [ ' + '''' + JobName + ''','''', ''' + JobStatus + ''', '
+ 'new Date(' + CAST(DATEPART(YEAR, SDT) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(MONTH, SDT) - 1 AS VARCHAR(4))
+ ', ' + CAST(DATEPART(DAY, SDT) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(HOUR, SDT) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(MINUTE, SDT) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(SECOND, SDT) AS VARCHAR(4)) + '), ' + 'new Date('
+ CAST(DATEPART(YEAR, EDT) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(MONTH, EDT) - 1 AS VARCHAR(4))
+ ', ' + CAST(DATEPART(DAY, EDT) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(HOUR, EDT) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(MINUTE, EDT) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(SECOND, EDT) AS VARCHAR(4)) + ') ],'
FROM #JobExecutionTimes
--###################################################################################################
--
-- Html Timeline - Last serverstart
--
--###################################################################################################
DECLARE @DTServerstart AS DATETIME
SET @DTServerstart = (SELECT login_time FROM sys.dm_exec_sessions WHERE session_id=1) --AND login_time >= @StartDate)
INSERT INTO ##TimelineGraph
(HTML
)
SELECT ' [ ' + '''' + 'Last serverstart' + ''','''', ''' + @COLOR_SERVERSTART + ''', '
+ 'new Date(' + CAST(DATEPART(YEAR, @DTServerstart) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(MONTH, @DTServerstart) - 1 AS VARCHAR(4))
+ ', ' + CAST(DATEPART(DAY, @DTServerstart) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(HOUR, @DTServerstart) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(MINUTE, @DTServerstart) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(SECOND, @DTServerstart) AS VARCHAR(4)) + '), ' + 'new Date('
+ CAST(DATEPART(YEAR, @DTServerstart) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(MONTH, @DTServerstart) - 1 AS VARCHAR(4))
+ ', ' + CAST(DATEPART(DAY, @DTServerstart) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(HOUR, @DTServerstart) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(MINUTE, @DTServerstart) AS VARCHAR(4)) + ', '
+ CAST(DATEPART(SECOND, @DTServerstart) AS VARCHAR(4)) + ') ],'
WHERE @DTServerstart >= @StartDate
--###################################################################################################
--
-- Html Timeline - Footer
--
--###################################################################################################
INSERT INTO ##TimelineGraph
(HTML
)
SELECT ' ]);
var options =
{
timeline: {
groupByRowLabel: true,
colorByRowLabel: false,
singleColor: false,
rowLabelStyle: {fontName: ''Helvetica'', fontSize: 11 },
barLabelStyle: {fontName: ''Helvetica'', fontSize: 7 }
},
hAxis: {format: "dd.MM.yyyy - HH:mm"}
};
chart.draw(dataTable, options);
'
--###################################################################################################
--
-- Html Timeline - Tooltip - Additional jobdata, needed because it's not possible to squezze this
-- into the google timeline data
--
--###################################################################################################
INSERT INTO ##TimelineGraph
(HTML
)
SELECT 'var dataTableAdditionalData = google.visualization.arrayToDataTable(['
INSERT INTO ##TimelineGraph
(HTML
)
SELECT '[''Addition1''],'
--Jobdata
INSERT INTO ##TimelineGraph
(HTML
)
SELECT '[' + '''' + LEFT(REPLACE(COALESCE(JobMessage, ''), '''', ''), 200) + '''],'
FROM #JobExecutionTimes
--Last serverstart
INSERT INTO ##TimelineGraph
(HTML
)
SELECT '[' + '''' + '' + '''],'
FROM sys.dm_exec_sessions
WHERE session_id =1
AND login_time >= @StartDate
--Dataend
INSERT INTO ##TimelineGraph
(HTML)
SELECT ' ]);'
--Javascript tooltip
INSERT INTO ##TimelineGraph
(HTML
)
SELECT ' google.visualization.events.addListener(chart, ''onmouseover'', function (e) {
setTooltipContent(dataTable, e.row);
});
function setTooltipContent(dataTable, row) {
if (row != null) {
var content = ''<div class="custom-tooltip" ><h3>'' + dataTable.getValue(row, 0) + ''</h3>'' +
''</div>'' +
''<div>Von '' + formatDate(dataTable.getValue(row, 3)) + '' bis '' + formatDate(dataTable.getValue(row, 4)) + ''</div>'' +
''<br/><div>Dauer: '' + (dateDiff(dataTable.getValue(row, 3), dataTable.getValue(row, 4))) + ''</div>'' +
''<br/><div>'' + (dataTableAdditionalData.getValue(row, 0)) + ''</div>''
;
var tooltip = document.getElementsByClassName("google-visualization-tooltip")[0];
tooltip.innerHTML = content;
}
}
'
INSERT INTO ##TimelineGraph
(HTML
)
SELECT '
function formatDate(d) {
return ("0" + d.getDate()).slice(-2) + "." + ("0" + (d.getMonth() + 1)).slice(-2) + "." + d.getFullYear() + " " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2) + ":" + ("0" + d.getSeconds()).slice(-2);
}
function dateDiff(dateNow, dateFuture) {
var seconds = Math.floor((dateFuture - (dateNow)) / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours = hours - (days * 24);
minutes = minutes - (days * 24 * 60) - (hours * 60);
seconds = seconds - (days * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60);
return ("0" + days).slice(-2) + '':'' +("0" + hours).slice(-2) + '':'' + ("0" + minutes).slice(-2) + '':'' + ("0" + seconds).slice(-2)
}
'
--###################################################################################################
--
-- Html Timeline - Footerend
--
--###################################################################################################
INSERT INTO ##TimelineGraph
(HTML
)
SELECT '}
</script>
</head>
<body>' + '<font face="Helvetica" size="3" ><b>' + @@servername
+ ' Jobs' + ' from ' + CONVERT(VARCHAR(20), @StartDate, 120)
+ ' to ' + CONVERT(VARCHAR(20), @EndDate, 120) +
+ CASE WHEN @ERROR_COUNT = 0 THEN ''
ELSE
'. ' + @ERROR_TEXT
END
+ CASE WHEN @MinimalJobDurationInSeconds = 0 THEN ''
ELSE ' (Jobs longer than '
+ CAST(@MinimalJobDurationInSeconds AS VARCHAR(10))
+ ' seconds)'
END
+ '</b></font>
<p/>
'
INSERT INTO ##TimelineGraph
(HTML
)
SELECT '
<div id="legend">
<ul>
legend:
<li>
<div class="legendSuccess"></div>= Successful
</li>
<li>
<div class="legendRunning"></div>= Running
</li>
<li>
<div class="legendError"></div>= Faulty
</li>
<li>
<div class="legendRetry"></div>= Retry
</li>
<li>
<div class="legendAborted"></div>= Aborted
</li>
<li>
<div class="legendServerstart"></div>= Last serverstart
'
INSERT INTO ##TimelineGraph
(HTML
)
SELECT ' ('
+ CONVERT(char(20), @DTServerstart,113)
+ ')
'
INSERT INTO ##TimelineGraph
(HTML
)
SELECT
'</li>
<li>
<div class="legendUndefined"></div>= Undefined
</li>
</ul>
</div>
'
--###################################################################################################
--
-- Html Timeline - End
--
--###################################################################################################
DECLARE @Timeline_Width AS INTEGER
IF @DaysPast < 1
SET @Timeline_Width = 1800
ELSE
SET @Timeline_Width = @DaysPast * 1800
INSERT INTO ##TimelineGraph
(HTML
)
SELECT '
<div id="JobTimeline" style="width: ' + CAST(@Timeline_Width AS VARCHAR(10))
+ 'px; height: 950px;"></div>
</body>
</html>'
--###################################################################################################
--
-- Send timeline as an email
--
--###################################################################################################
DECLARE @emailBodyText NVARCHAR(MAX);
SET @emailBodyText = 'Timeline of all jobs between '
+ CONVERT(VARCHAR(20), @StartDate, 120) + ' to '
+ CONVERT(VARCHAR(20), @EndDate, 120) + ' open the attachement.'
DECLARE @emailSubjectText NVARCHAR(MAX);
SET @emailSubjectText = @@servername + ' Jons between '
+ CONVERT(VARCHAR(20), @StartDate, 120) + ' and '
+ CONVERT(VARCHAR(20), @EndDate, 120)
+ ' ' + @ERROR_TEXT
DECLARE @emailHTMLFilenameText NVARCHAR(MAX);
SET @emailHTMLFilenameText = @@servername + ' Jobs between '
+ CONVERT(VARCHAR(20), @StartDate, 120) + ' and '
+ CONVERT(VARCHAR(20), @EndDate, 120) + '.html'
SET @emailHTMLFilenameText = REPLACE(@emailHTMLFilenameText, ':', '_')
DECLARE @email_Importance NVARCHAR(10);
IF @ERROR_COUNT > 0
SET @email_Importance = 'High'
ELSE
SET @email_Importance = 'Normal'
EXECUTE msdb.dbo.sp_send_dbmail @recipients = @EmailTo,
@subject = @emailSubjectText, @body = @emailBodyText,
@body_format = 'HTML' -- or TEXT
, @importance = @email_Importance
, @sensitivity = 'Normal' --Normal Personal Private Confidential
, @execute_query_database = 'master', @query_result_header = 0, --@query_result_header = 0 is important, otherwise "HTML----" comes from the query into the html
@query = 'set nocount on; SELECT HTML FROM ##TimelineGraph ORDER BY ID',
@query_result_no_padding = 1
--,@query_no_truncate= 1
, @attach_query_result_as_file = 1,
@query_attachment_filename = @emailHTMLFilenameText
GOTO Cleanup
--###################################################################################################
--
-- Nothing to do or errorhandling
--
--###################################################################################################
NothingToDo:
PRINT 'Found no jobs (this could be also an error)'
--###################################################################################################
--
-- Cleanup
--
--###################################################################################################
Cleanup:
IF OBJECT_ID('tempdb..#JobExecutionTimes') IS NOT NULL
DROP TABLE #JobExecutionTimes;
IF OBJECT_ID('tempdb..##TimelineGraph') IS NOT NULL
DROP TABLE ##TimelineGraph;