|
1 | 1 |
|
2 | 2 | -- SQL Server 2008 R2 Diagnostic Information Queries
|
3 | 3 | -- Glenn Berry
|
4 |
| --- Last Modified: January 3, 2023 |
| 4 | +-- Last Modified: March 6, 2024 |
5 | 5 | -- https://glennsqlperformance.com/
|
6 | 6 | -- https://sqlserverperformance.wordpress.com/
|
7 | 7 | -- YouTube: https://bit.ly/2PkoAM1
|
|
14 | 14 | -- Please make sure you are using the correct version of these diagnostic queries for your version of SQL Server
|
15 | 15 |
|
16 | 16 | --******************************************************************************
|
17 |
| ---* Copyright (C) 2023 Glenn Berry |
| 17 | +--* Copyright (C) 2024 Glenn Berry |
18 | 18 | --* All rights reserved.
|
19 | 19 | --*
|
20 | 20 | --*
|
@@ -139,18 +139,24 @@ SERVERPROPERTY('IsIntegratedSecurityOnly') AS [IsIntegratedSecurityOnly];
|
139 | 139 |
|
140 | 140 |
|
141 | 141 | -- Get SQL Server Agent jobs and Category information (Query 4) (SQL Server Agent Jobs)
|
142 |
| -SELECT sj.name AS [Job Name], sj.[description] AS [Job Description], SUSER_SNAME(sj.owner_sid) AS [Job Owner], |
| 142 | +SELECT sj.name AS [Job Name], sj.[description] AS [Job Description], |
| 143 | +sc.name AS [CategoryName], SUSER_SNAME(sj.owner_sid) AS [Job Owner], |
143 | 144 | sj.date_created AS [Date Created], sj.[enabled] AS [Job Enabled],
|
144 |
| -sj.notify_email_operator_id, sj.notify_level_email, sc.name AS [CategoryName], |
145 |
| -s.[enabled] AS [Sched Enabled], js.next_run_date, js.next_run_time |
| 145 | +sj.notify_email_operator_id, sj.notify_level_email, h.run_status, |
| 146 | +RIGHT(STUFF(STUFF(REPLACE(STR(h.run_duration, 7, 0), ' ', '0'), 4, 0, ':'), 7, 0, ':'),8) AS [Last Duration - HHMMSS], |
| 147 | +CONVERT(DATETIME, RTRIM(h.run_date) + ' ' + STUFF(STUFF(REPLACE(STR(RTRIM(h.run_time),6,0),' ','0'),3,0,':'),6,0,':')) AS [Last Start Date] |
146 | 148 | FROM msdb.dbo.sysjobs AS sj WITH (NOLOCK)
|
147 |
| -INNER JOIN msdb.dbo.syscategories AS sc WITH (NOLOCK) |
| 149 | +LEFT OUTER JOIN |
| 150 | + (SELECT job_id, instance_id = MAX(instance_id) |
| 151 | + FROM msdb.dbo.sysjobhistory WITH (NOLOCK) |
| 152 | + GROUP BY job_id) AS l |
| 153 | +ON sj.job_id = l.job_id |
| 154 | +LEFT OUTER JOIN msdb.dbo.syscategories AS sc WITH (NOLOCK) |
148 | 155 | ON sj.category_id = sc.category_id
|
149 |
| -LEFT OUTER JOIN msdb.dbo.sysjobschedules AS js WITH (NOLOCK) |
150 |
| -ON sj.job_id = js.job_id |
151 |
| -LEFT OUTER JOIN msdb.dbo.sysschedules AS s WITH (NOLOCK) |
152 |
| -ON js.schedule_id = s.schedule_id |
153 |
| -ORDER BY sj.name OPTION (RECOMPILE); |
| 156 | +LEFT OUTER JOIN msdb.dbo.sysjobhistory AS h WITH (NOLOCK) |
| 157 | +ON h.job_id = l.job_id |
| 158 | +AND h.instance_id = l.instance_id |
| 159 | +ORDER BY CONVERT(INT, h.run_duration) DESC, [Last Start Date] DESC OPTION (RECOMPILE); |
154 | 160 | ------
|
155 | 161 |
|
156 | 162 | -- Gives you some basic information about your SQL Server Agent jobs, who owns them and how they are configured
|
|
0 commit comments