How Can I Setup a SQL Job Step Alert on SQL 2016?

Tessando
Tessando used Ask the Experts™
on
I am running SQL Server 2016 on Windows Server 2016 and have two large SQL Jobs. One runs in the early morning and one in the evening.

I've already setup Database mail and have tested successfully, so I get email alerts for completed jobs. I'd like to go a layer deeper.

I'd like to create an email alert if a job runs over a certain amount of time. There are 15 steps in this particular SQL Job, but a handful of them get hung-up often (mostly because some folks are allowed to upload Excel docs that the Job updates).

Is there a way to create a New Alert such that a given job step would send me a notification if it took over a certain amount of time? (e.g. ~30 minutes).

Thanks for your help.


...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
There is no built-in alert in SQL that does that.  You'd have to write code yourself to do that.
Database Developer
Commented:
You can set a second job that runs at a specific time (say 2 hours after the first job) and checks to see if the first job is still running. If the first job is still running, the second job can send out an alert, otherwise, it just quits, reporting success and does nothing.
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
I'd start the monitoring job within the main job.  That way you can change the main job schedule without having to remember to adjust other job(s).

The other job could delay the amount of time passed to it to delay, such as 120 minutes, or just always delay a fixed amount of time, whichever you prefer (I like the first method, but you may prefer the second).
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Doug BishopDatabase Developer

Commented:
Scott: Have you found a way to pass a parameter to a job?
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
Yep.  Update the description on the receiving job by concatenating the control values to the existing desc.  When that job starts, it reads its own description (from msdb.dbo.sysjobs) to get the passed info.  Something like DELAY=120 should be sufficient, and would easily allow values to be passed too.  The receiving can also remove the params it used, to free space for the next time it needs called.

I thought maybe with SQL 2016 this wouldn't be necessary, because  of SESSION_CONTEXT(), but I realized the new job would be a new session, so that wouldn't work and would have to do it my "old school" way.
Doug BishopDatabase Developer

Commented:
I think he is going to have to kick off the second job from the first, using sp_start_job in the first step, and the second job is going to have to have a WAITFOR DELAY '02:00:00' (e.g. to wait 2 hours) before checking to see if the first job is still running.
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
Exactly, as I stated.  Except that I would also pass the delay interval in, in case it changes in the future, or based on the specific data being processed that run.  Or if for some reason the first job started late, or to control other aspects of the second job, etc..
TessandoIT Administrator

Author

Commented:
Thanks for everyone's suggestions. I wish this was something that Microsoft had built in, but this work-around is going to fit in nicely. Thanks again for your contribution.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial