-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJob_Report.sql
96 lines (85 loc) · 3.18 KB
/
Job_Report.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
USE [msdb];
GO
/*====================================================================================================
Job Report - Cortland Goffena
Shows current status of job and history of the job for comparisons
====================================================================================================*/
DECLARE @JobName NVARCHAR(255) = 'JobName'
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
DROP TABLE IF EXISTS #History
SELECT TOP 100
sj.name AS JobName,
sj.enabled AS JobEnabled,
CONVERT (DATE, CONVERT (CHAR(8), sh.run_date), 112) AS StartDate,
'Not Running' AS JobStatus,
CASE
WHEN sh.run_status = 0 THEN 'Failed'
WHEN sh.run_status = 1 THEN 'Succeeded'
WHEN sh.run_status = 2 THEN 'Retry'
WHEN sh.run_status = 3 THEN 'Canceled'
WHEN sh.run_status = 4 THEN 'In Progress'
END AS JobStatusDetails,
1 AS JobFinished,
CAST( dbo.agent_datetime( sh.run_date, sh.run_time ) AS TIME(2) ) AS StartTime,
CAST( DATEADD(
SECOND, dbo.fn_Agent_DurationSeconds( sh.run_duration ),
dbo.agent_datetime( sh.run_date, sh.run_time )
) AS TIME(2) ) AS EndTime,
RIGHT( '0' + CAST( dbo.fn_Agent_DurationSeconds( sh.run_duration ) / 3600 AS NVARCHAR(2) ),2 ) + ':' +
RIGHT( '0' + CAST( ( dbo.fn_Agent_DurationSeconds( sh.run_duration ) / 60 ) % 60 AS VARCHAR(2) ),2 ) + ':' +
RIGHT( '0' + CAST( dbo.fn_Agent_DurationSeconds( sh.run_duration ) % 60 AS VARCHAR(2) ), 2 ) AS RunTime
INTO #JobHistory
FROM dbo.sysjobs AS sj
INNER JOIN dbo.sysjobhistory AS sh
ON sh.job_id = sj.job_id
WHERE sh.step_id=0
AND sj.name = @JobName
ORDER BY dbo.agent_datetime(sh.run_date, sh.run_time) DESC
DROP TABLE IF EXISTS #JobActivity
SELECT
sj.name AS JobName,
sj.enabled AS JobEnabled,
CAST( sa.start_execution_date AS DATE ) AS StartDate,
'Running' AS JobStatus,
'Step ' + CAST( sjs.step_id AS VARCHAR(3) ) + ': ' + sjs.step_name AS JobStatusDetails,
0 AS JobFinished,
CAST( sa.start_execution_date AS TIME(2) ) AS StartTime,
CAST( '99:99:99.00' AS NVARCHAR(11) ) AS EndTime,
RIGHT( '0' + CAST( DATEDIFF( SECOND, sa.start_execution_date, CAST( GETDATE() AS DATETIME ) ) / 3600 AS NVARCHAR(2) ), 2 ) + ':' +
RIGHT( '0' + CAST( ( DATEDIFF( SECOND, sa.start_execution_date, CAST( GETDATE() AS DATETIME ) ) / 60 ) % 60 AS VARCHAR(2) ), 2 ) + ':' +
RIGHT( '0' + CAST( DATEDIFF( SECOND, sa.start_execution_date, CAST( GETDATE() AS DATETIME ) ) % 60 AS VARCHAR(2) ), 2 ) AS RunTime
INTO #JobActivity
FROM dbo.sysjobs AS sj
INNER JOIN dbo.sysjobactivity AS sa
ON SA.job_id = SJ.job_id
INNER JOIN dbo.sysjobsteps AS sjs
ON sjs.job_id = sa.job_id
AND sjs.step_id = ISNULL( sa.last_executed_step_id, 0 ) + 1
WHERE SA.session_id = ( SELECT MAX( session_id ) FROM msdb.dbo.sysjobactivity )
AND SJ.name = @JobName
AND sa.start_execution_date IS NOT NULL
AND sa.stop_execution_date IS NULL
SELECT
JobName,
JobEnabled,
StartDate,
JobStatus,
JobStatusDetails,
JobFinished,
CAST(StartTime AS NVARCHAR(11)) AS StartTime,
CAST(EndTime AS NVARCHAR(11)) AS EndTime,
RunTime
FROM #JobHistory
UNION ALL
SELECT
JobName,
JobEnabled,
StartDate,
JobStatus,
JobStatusDetails,
JobFinished,
CAST(StartTime AS NVARCHAR(11)) AS StartTime,
EndTime,
RunTime
FROM #JobActivity
ORDER BY StartDate DESC, StartTime DESC