in the previous Post i finished the Part of How to Diagnosis MSSQL Server or MSSQL Database with fast way..? but to be honest with you this really very big part and can’t Finished it coz this need much T-SQL , Professional Third party tool like dynatrace really wonderful Tool don’t miss any corn.
So today i will start new Part in our Series How to Administrate and Monitor SQL Server Jobs by T-SQL..?
So let’s go to Start
WHAT YOU NEED in SQL SERVER JOB WE COVERED HERE
First thing any DBA need to monitor the SQL Agent at the first HE/SHE need to know the First Vision in his/here SQL Server jobs
Check SQL Server jobs First Vision..?
SELECT DISTINCT substring (a. name ,1,100) AS [Job Name ], |
WHEN a.enabled = 0 THEN 'No' |
WHEN a.enabled = 1 THEN 'Yes' |
substring (b. name ,1,30) AS [ Name of the schedule], |
'Frequency of the schedule execution' = case |
WHEN b.freq_type = 1 THEN 'Once' |
WHEN b.freq_type = 4 THEN 'Daily' |
WHEN b.freq_type = 8 THEN 'Weekly' |
WHEN b.freq_type = 16 THEN 'Monthly' |
WHEN b.freq_type = 32 THEN 'Monthly relative' |
WHEN b.freq_type = 32 THEN 'Execute when SQL Server Agent starts' |
'Units for the freq_subday_interval' = case |
WHEN b.freq_subday_type = 1 THEN 'At the specified time' |
WHEN b.freq_subday_type = 2 THEN 'Seconds' |
WHEN b.freq_subday_type = 4 THEN 'Minutes' |
WHEN b.freq_subday_type = 8 THEN 'Hours' |
cast ( cast (b.active_start_date as varchar (15)) as datetime) as active_start_date, |
cast ( cast (b.active_end_date as varchar (15)) as datetime) as active_end_date, |
Stuff(Stuff( right ( '000000' + Cast (c.next_run_time as Varchar ),6),3,0, ':' ),6,0, ':' ) as Run_Time, |
convert ( varchar (24),b.date_created) as Created_Date |
INNER JOIN msdb.dbo.sysJobschedules c ON a.job_id = c.job_id |
INNER JOIN msdb.dbo.SysSchedules b on b.Schedule_id=c.Schedule_id |
This Script will return to you :
Job Name / Enabled or Disabled / Name of the scheduled/ Frequency of the schedule execution / Units for the freq_subday_interval/ active_start_date/ active_end_date/ Run_Time /Created_Date.
From this Script we can do most Customization
Check jobs Status ..? Is it enabled or Disabled ..?
SELECT DISTINCT substring (a. name ,1,100) AS [Job Name ], |
WHEN a.enabled = 0 THEN 'No' |
WHEN a.enabled = 1 THEN 'Yes' |
When was a SQL Agent job was created or when was the last time it was modified?
if you need to know this info the the date a SQL Agent job was created or the last time it was modified, you can find the information in the sysjobs table in the MSDB database.
Identify newly created SQL Server Agent Jobs
this Script will return the Jobs created in last 15 day
SELECT [ Name ], [Date_Created] |
WHERE [Date_Created] BETWEEN DATEADD(dd, -15, GETDATE()) AND GETDATE(); |
Check enabled jobs Without notification setup ..?
Jobs nightmare Really from past Experience this very Important point IF your jobs is enabled notification or no ? sure it must be YES Enabled to be know what happened in your Jobs to don’t see nightmare.
SELECT [ Name ], [Date_Created], 'Job Enabled..?' = case |
WHEN enabled = 0 THEN 'No' |
WHEN enabled = 1 THEN 'Yes' |
'notification Enabled..?' = case |
WHEN Notify_Level_Email = 0 THEN 'No' |
WHEN Notify_Level_Email = 1 THEN 'Yes' |
WHERE [Notify_Level_Email] = 0 |
After this Script What you should do if you have some jobs without Notification setup at this time waste your time go direct to update this jobs by the Notification setup.so at the first you should know what the Operator you will pass in the Notification
retrieving enabled operators in SQL Server Agent
SELECT [ID], [ Name ], [Enabled] |
FROM MSDB.dbo.sysoperators |
Now after you select one from sysoperators you should go yo update your Jobs
Update SQL Server Agent Jobs without any notification by New notification
SET S.[notify_level_email] = 2, |
S.[notify_email_operator_id] = 1 |
WHERE S.[Notify_Level_Email] = 0 |
SQL Server Agent Job Setup and Configuration Information..
[sJOB].[job_id] AS [JobID] |
, [sJOB].[ name ] AS [JobName] |
, [sDBP].[ name ] AS [JobOwner] |
, [sCAT].[ name ] AS [JobCategory] |
, [sJOB].[description] AS [JobDescription] |
, [sJOB].[date_created] AS [JobCreatedOn] |
, [sJOB].[date_modified] AS [JobLastModifiedOn] |
, [sSVR].[ name ] AS [OriginatingServerName] |
, [sJSTP].[step_id] AS [JobStartStepNo] |
, [sJSTP].[step_name] AS [JobStartStepName] |
WHEN [sSCH].[schedule_uid] IS NULL THEN 'No' |
, [sSCH].[schedule_uid] AS [JobScheduleID] |
, [sSCH].[ name ] AS [JobScheduleName] |
, CASE [sJOB].[delete_level] |
WHEN 3 THEN 'On Completion' |
END AS [JobDeletionCriterion] |
[msdb].[dbo].[sysjobs] AS [sJOB] |
LEFT JOIN [msdb].[sys].[servers] AS [sSVR] |
ON [sJOB].[originating_server_id] = [sSVR].[server_id] |
LEFT JOIN [msdb].[dbo].[syscategories] AS [sCAT] |
ON [sJOB].[category_id] = [sCAT].[category_id] |
LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sJSTP] |
ON [sJOB].[job_id] = [sJSTP].[job_id] |
AND [sJOB].[start_step_id] = [sJSTP].[step_id] |
LEFT JOIN [msdb].[sys].[database_principals] AS [sDBP] |
ON [sJOB].[owner_sid] = [sDBP].[sid] |
LEFT JOIN [msdb].[dbo].[sysjobschedules] AS [sJOBSCH] |
ON [sJOB].[job_id] = [sJOBSCH].[job_id] |
LEFT JOIN [msdb].[dbo].[sysschedules] AS [sSCH] |
ON [sJOBSCH].[schedule_id] = [sSCH].[schedule_id] |
Check Jobs without Schedule ..
Select Name AS [ No schedule in this Job] |
from [msdb]..[sysjobs] where job_id not in |
( Select job_id from MSDB..sysjobschedules ) |
Check jobs With Enabled Schedule ..
Select [sJOB].[ name ] AS [JobName], |
When [sSCH].[schedule_uid] IS Null THEN 'NO' |
End AS 'IS schedules Enabled..?' |
[msdb].[dbo].[sysjobs] AS [sJOB] |
Inner Join [msdb].[dbo].[sysjobschedules] [sJOBSCH] |
On [sJOBSCH].job_id = [sJOB].job_id |
Inner Join [sysschedules] AS [sSCH] |
On [sSCH].schedule_id=[sJOBSCH].schedule_id |
Now after we determined what is the jobs have Schedule and what is no Actually we need to know the Schedule Information for each job.
Check SQL Server Agent Job Schedule Information..
[schedule_uid] AS [ScheduleID] |
, [ name ] AS [ScheduleName] |
WHEN [freq_type] = 64 THEN 'Start automatically when SQL Server Agent starts' |
WHEN [freq_type] = 128 THEN 'Start whenever the CPUs become idle' |
WHEN [freq_type] IN (4,8,16,32) THEN 'Recurring' |
WHEN [freq_type] = 1 THEN 'One Time' |
WHEN 32 THEN 'Monthly - Relative to Frequency Interval' |
WHEN 64 THEN 'Start automatically when SQL Server Agent starts' |
WHEN 128 THEN 'Start whenever the CPUs become idle' |
WHEN 4 THEN 'Occurs every ' + CAST ([freq_interval] AS VARCHAR (3)) + ' day(s)' |
WHEN 8 THEN 'Occurs every ' + CAST ([freq_recurrence_factor] AS VARCHAR (3)) |
+ CASE WHEN [freq_interval] & 1 = 1 THEN 'Sunday' ELSE '' END |
+ CASE WHEN [freq_interval] & 2 = 2 THEN ', Monday' ELSE '' END |
+ CASE WHEN [freq_interval] & 4 = 4 THEN ', Tuesday' ELSE '' END |
+ CASE WHEN [freq_interval] & 8 = 8 THEN ', Wednesday' ELSE '' END |
+ CASE WHEN [freq_interval] & 16 = 16 THEN ', Thursday' ELSE '' END |
+ CASE WHEN [freq_interval] & 32 = 32 THEN ', Friday' ELSE '' END |
+ CASE WHEN [freq_interval] & 64 = 64 THEN ', Saturday' ELSE '' END |
WHEN 16 THEN 'Occurs on Day ' + CAST ([freq_interval] AS VARCHAR (3)) |
+ CAST ([freq_recurrence_factor] AS VARCHAR (3)) + ' month(s)' |
WHEN 32 THEN 'Occurs on ' |
+ CASE [freq_relative_interval] |
WHEN 10 THEN 'Weekend day' |
+ ' of every ' + CAST ([freq_recurrence_factor] AS VARCHAR (3)) |
, CASE [freq_subday_type] |
WHEN 1 THEN 'Occurs once at ' |
STUFF( RIGHT ( '000000' + CAST ([active_start_time] AS VARCHAR (6)), 6) |
WHEN 2 THEN 'Occurs every ' |
+ CAST ([freq_subday_interval] AS VARCHAR (3)) + ' Second(s) between ' |
STUFF( RIGHT ( '000000' + CAST ([active_start_time] AS VARCHAR (6)), 6) |
STUFF( RIGHT ( '000000' + CAST ([active_end_time] AS VARCHAR (6)), 6) |
WHEN 4 THEN 'Occurs every ' |
+ CAST ([freq_subday_interval] AS VARCHAR (3)) + ' Minute(s) between ' |
STUFF( RIGHT ( '000000' + CAST ([active_start_time] AS VARCHAR (6)), 6) |
STUFF( RIGHT ( '000000' + CAST ([active_end_time] AS VARCHAR (6)), 6) |
WHEN 8 THEN 'Occurs every ' |
+ CAST ([freq_subday_interval] AS VARCHAR (3)) + ' Hour(s) between ' |
STUFF( RIGHT ( '000000' + CAST ([active_start_time] AS VARCHAR (6)), 6) |
STUFF( RIGHT ( '000000' + CAST ([active_end_time] AS VARCHAR (6)), 6) |
STUFF( CAST ([active_start_date] AS VARCHAR (8)), 5, 0, '-' ) |
, 8, 0, '-' ) AS [ScheduleUsageStartDate] |
STUFF( CAST ([active_end_date] AS VARCHAR (8)), 5, 0, '-' ) |
, 8, 0, '-' ) AS [ScheduleUsageEndDate] |
, [date_created] AS [ScheduleCreatedOn] |
, [date_modified] AS [ScheduleLastModifiedOn] |
FROM [msdb].[dbo].[sysschedules] |
this not the end
Do not go here and there Check your mobile notification daily check your email notification daily check our blog
Follow Me to see my new posts and to complete with me this Amazing part How to Administrate and Monitor SQL Server Jobs by T-SQL..?
Like this:
Like Loading...
Related
Published by Mustafa EL-Masry
I am Microsoft database consultant working as a Database administrator for more than +10 Years I have very good knowledge about Database Migration, Consolidation, Performance Tuning, Automation Using T-SQL, and PowerShell and so many other tasks I do it in multiple customers here in KSA and as of now, I am working in Bank Albilad managing the core banking system that is hosted in SQL Server Database 8 TB. Also, I am Microsoft certified 2008 and 2016 in SQL Server (2x MCTS, 2x MCTIP, MCSA, MCSE) and I am Microsoft Certified Trainer (MCT) also I am azure Certified (AZ-900, AZ-103) also I was awarded by Microsoft Azure Heroes 3 times as (Azure Content hero, Azure Community hero and Azure Mentor) For more information check my page
https://mostafaelmasry.com/about-me/
View all posts by Mustafa EL-Masry