I am trying to work out the TSQL to detect if a JOB has overran and missed its next schedule.
Scenario: Job is scheduled to run every hour (there may be more than one schedule enabled), but due to some reason such as network delays, the job stops taking 10 minutes and is now running for 90 minutes and therefore missing it's hourly start.
There is a table called msdb.dbo.sysjobactivity, which has a column called next_scheduled_run_date but this date only gets update after the currently running job has finished. This is the same behaviour as the Job Activity monitor, the next run date only updates after the current job finishes.
msdb.dbo.sysjobactivity is updated as per the details in: https://dba.stackexchange.com/questions/22644/how-and-when-does-sql-agent-update-next-run-date-next-run-time-values
Question: How can I calculate the next scheduled run for a job, based on the current job start time while the job is running. There may be more than a single schedule assigned.
Any pointers appreciated.
This is a tricky one. Firstly the stackexchange link that you included is excellent. I followed that as well as doing my own trace. The code below will set you in the right direction. You may need to do detailed testing, I found that I had some timing issues when the answer was to the same minute but the seconds were slightly out. You can of course modify this as needed.
here is the first draft of the code which should put you in the right direction