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

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.


...
TessandoIT AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
There is no built-in alert in SQL that does that.  You'd have to write code yourself to do that.
Doug BishopDatabase DeveloperCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
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).
Determine the Perfect Price for Your IT Services

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

Doug BishopDatabase DeveloperCommented:
Scott: Have you found a way to pass a parameter to a job?
Scott PletcherSenior DBACommented:
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 DeveloperCommented:
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 DBACommented:
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 AdministratorAuthor 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.