0% found this document useful (0 votes)
4 views3 pages

Job Details Query

The document contains a SQL query that retrieves job schedules and details from a SQL Server database. It includes information about job names, statuses, scheduling frequencies, and average and maximum durations of job executions. The query organizes the data by job names and schedules, providing a comprehensive overview of job management in SQL Server.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Job Details Query

The document contains a SQL query that retrieves job schedules and details from a SQL Server database. It includes information about job names, statuses, scheduling frequencies, and average and maximum durations of job executions. The query organizes the data by job names and schedules, providing a comprehensive overview of job management in SQL Server.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

WITH JobSchedules AS (

SELECT
schedule_id, [name], [enabled],
CASE freq_type
WHEN 1 THEN 'One time only'
WHEN 4 THEN 'Daily'
WHEN 8 THEN 'Weekly'
WHEN 16 THEN 'Monthly'
WHEN 32 THEN 'Monthly'
WHEN 64 THEN 'When SQL Server Agent starts'
WHEN 128 THEN 'When computer is idle'
END AS Frequency,
IIF(freq_type = 32 AND freq_relative_interval <> 0,
CASE freq_relative_interval
WHEN 1 THEN 'First '
WHEN 2 THEN 'Second '
WHEN 4 THEN 'Third '
WHEN 8 THEN 'Fourth '
WHEN 16 THEN 'Last '
END,
''
) +
CASE freq_type
WHEN 1 THEN ''
WHEN 4 THEN IIF(freq_interval = 1, 'Every day', 'Every ' + CAST(freq_interval
AS VARCHAR(3)) + ' day(s)')
WHEN 8 THEN
IIF(freq_interval & 2 = 2, 'Mon ', '') +
IIF(freq_interval & 4 = 4, 'Tue ', '') +
IIF(freq_interval & 8 = 8, 'Wed ', '') +
IIF(freq_interval & 16 = 16, 'Thu ', '') +
IIF(freq_interval & 32 = 32, 'Fri ', '') +
IIF(freq_interval & 64 = 64, 'Sat ', '') +
IIF(freq_interval & 1 = 1, 'Sun ', '')
WHEN 16 THEN 'On the ' + CAST(freq_interval AS VARCHAR(3)) + 'st day of the
month'
WHEN 32 THEN
CASE freq_interval
WHEN 1 THEN 'Sunday'
WHEN 2 THEN 'Monday'
WHEN 3 THEN 'Tuesday'
WHEN 4 THEN 'Wednesday'
WHEN 5 THEN 'Thursday'
WHEN 6 THEN 'Friday'
WHEN 7 THEN 'Saturday'
WHEN 8 THEN 'Day'
WHEN 9 THEN 'Weekday'
WHEN 10 THEN 'Weekend day'
END
WHEN 64 THEN ''
WHEN 128 THEN ''
END AS DayInterval,
IIF(freq_subday_interval <> 0,
CASE freq_subday_type
WHEN 1 THEN 'At ' + STUFF(STUFF(RIGHT('00000' + CAST(active_start_time AS
VARCHAR(6)), 6), 3, 0, ':'), 6, 0, ':')
WHEN 2 THEN ' every ' + CAST(freq_subday_interval AS VARCHAR(3)) + '
seconds between '
WHEN 4 THEN 'every ' + CAST(freq_subday_interval AS VARCHAR(3)) + '
minutes between '
WHEN 8 THEN 'every ' + CAST(freq_subday_interval AS VARCHAR(3)) + ' hours
between '
END,
''
) AS DailyFrequency,
CASE
WHEN freq_type = 8 THEN ' every ' + CAST(freq_recurrence_factor AS
VARCHAR(3)) + ' week(s).'
WHEN freq_type IN (16, 32) THEN 'every ' + CAST(freq_recurrence_factor AS
VARCHAR(3)) + ' month(s).'
ELSE ''
END AS Recurrence,
STUFF(STUFF(RIGHT('00000' + CAST(active_start_time AS VARCHAR(6)), 6), 3, 0,
':'), 6, 0, ':') AS StartTime,
STUFF(STUFF(RIGHT('00000' + CAST(active_end_time AS VARCHAR(6)), 6), 3, 0,
':'), 6, 0, ':') AS EndTime,
CONVERT(VARCHAR(10), date_created, 103) AS Created
FROM msdb.dbo.sysschedules
)
SELECT
case when LEN(J.[name]) - LEN(REPLACE(J.[name], '_', ''))=0 then J.[name] else
case when LEN(J.[name]) - LEN(REPLACE(J.[name], '_', '')) <=2
then J.[name] else
LEFT(J.[name],
CHARINDEX('_', J.[name], CHARINDEX('_', J.[name], CHARINDEX('_', J.[name]) +
1) + 1) - 1) end end
AS DbName,
J.[name] AS JobName,
step.step_name,
step.command,
CASE
WHEN J.[enabled] = 0 THEN 'Disable'
ELSE 'Enable'
END AS Job_Status,
CASE
WHEN JS.[enabled] = 1 THEN
CONCAT(
'Occurs ', ISNULL(JS.[DayInterval], ''), ' ',
CASE
WHEN JS.[DailyFrequency] LIKE '%between' THEN JS.[DailyFrequency] + JS.
[StartTime] + ' and ' + JS.[EndTime]
ELSE JS.[DailyFrequency] + ' at ' + JS.[StartTime]
END,
'. Schedule will be used starting on ', Created
)
ELSE 'Not scheduled'
END AS Schedule_Details,

CONVERT(varchar, DATEADD(second, ROUND([jobhistory].[AvgDuration], 0), 0),


108) as Avg_duration ,
RIGHT('00' + CAST((maxdur.run_duration / 10000) AS VARCHAR), 2) + ':' +
RIGHT('00' + CAST((maxdur.run_duration / 100 % 100) AS VARCHAR), 2) + ':' +
RIGHT('00' + CAST((maxdur.run_duration % 100) AS VARCHAR), 2) AS [Max_duration]

FROM msdb.dbo.sysjobs AS J
LEFT JOIN sys.server_principals AS SP ON J.owner_sid = SP.[sid]
LEFT JOIN msdb.dbo.sysjobschedules AS JJS ON J.job_id = JJS.job_id
LEFT JOIN JobSchedules AS JS ON JJS.schedule_id = JS.schedule_id
LEFT JOIN [msdb].dbo.sysjobsteps step ON j.job_id = step.job_id
LEFT OUTER JOIN (
SELECT job_id, MAX(run_duration) AS run_duration
FROM msdb.dbo.sysjobhistory
GROUP BY job_id
) maxdur ON j.job_id = maxdur.job_id
LEFT OUTER JOIN (
SELECT
[job_id],
[AvgDuration] = SUM((([run_duration] / 10000 * 3600) + (([run_duration] %
10000) / 100 * 60) + ([run_duration] % 10000) % 100)) * 1.0 / COUNT([job_id])
FROM [msdb].[dbo].[sysjobhistory] WITH (NOLOCK)
WHERE [step_id] = 0
GROUP BY [job_id]
) AS [jobhistory] ON [jobhistory].[job_id] = J.[job_id]
ORDER BY J.[name] ASC;

You might also like