If it's an option, instead of running the job periodically throughout the day, you might consider kicking the job off once and putting the checking for the IF condition in a
So, let's say the job kicks off once daily at 12am.
The following TSQL script example would be placed inside the job step and the job would not actually complete until the
IF condition has been satisfied.
My example is checking to see if the current time is greater than 4:00pm. This is just for demo purposes. The actual code would be to check for whatever satisfies the
IF condition. If the condition is not satisfied, we simply
WAITFOR DELAY and continue in the
WHILE loop. My example checks for the
IF condition every 10 seconds, but you can put whatever delay you want.
You could even include checks (in the
WHILE loop) to see if the job has run past a specific time. In that case, you could send a warning email indicating early shutdown and exit the
WHILE loop ending today's run of the job.
DECLARE @IfConditionSatisfied BIT = 0
WHILE @IfConditionSatisfied = 0
IF convert(TIME(0), GETDATE()) > '16:00:00' --simulate the criteria that satisfied the IF condition
SET @IfConditionSatisfied = 1
--send emails, etc.
WAITFOR DELAY '00:00:10' --wait for 10 seconds (or specify other hours, minutes, seconds)