Send email with job step's failure message from within the job itself

Naomi Nosonovsky 8,331 Reputation points
2021-05-28T19:38:17.377+00:00

Hi everybody,

I'm wondering if it's even possible to setup a job that will try to execute something and if it fails, it will send information about failed step directly from the job?

I read this excellent blog post

https://www.sqlshack.com/reporting-and-alerting-on-job-failure-in-sql-server/

I tried to simplify everything and right now I'm using this code which seems to be sending information about previously failed job step instead of the job I'm executing:

declare @LastStepErrorMessage varchar(max) = (select top(1) message FROM MSDB.dbo.sysjobhistory h
 WHERE h.run_status = 0
 AND h.step_id > 0 order by run_date DESC);

I think the problem is that the job didn't complete yet when I'm trying to send the email from that same job itself. Do you see a solution to this problem or how should I code getting information about failed step(s) in the currently executing job?

Thanks a lot in advance.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,494 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,703 questions
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 120.2K Reputation points MVP
    2021-05-28T21:42:58.6+00:00

    The way you have written the query, you will get the message for any failed job on that same day. You need at least add run_time to the ORDER BY clause.

    It can also be a safety measure to restrict the query to the current job. You don't have to hard-code the job name, but you can use a job token for this. See https://learn.microsoft.com/en-us/sql/ssms/agent/use-tokens-in-job-steps?redirectedfrom=MSDN&view=sql-server-ver15.

    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Tom Phillips 17,761 Reputation points
    2021-05-28T20:36:47.013+00:00

    Rather than doing it that way, I use a process like this. Setup a job which runs a proc every 5 mins and emails the results of all jobs.

    https://www.sqlshack.com/how-to-create-email-alert-sql-server-agent-for-job-failures/

    1 person found this answer helpful.
    0 comments No comments

  2. Erland Sommarskog 120.2K Reputation points MVP
    2021-06-01T21:21:24.223+00:00

    I already switched the code, but I'll re-try later today. For each of the tokens I tried I was getting 'Variable A-SVR (or other names not found' when I was trying to run the job manually.

    What do you mean with "run manually"? If you run the code in a query window, it will not work, as this token replacement is something Agent does.

    So unfortunately the message is defined as nvarchar(4000) in the sysjobhistory table and therefore it is truncated (and not sure why SSIS returns so much extra unhelpful verbiage).

    It is usually best to redirect output to a file, which you do on the Advanced page of the job step definition. In theory, you could have the second step to read that file and mail it. That would better be a PowerShell step, I think.

    Or you could opt to send the truncated error message, and add a line to refer to the full log file. (Of which the name is in sysjobsteps.)

    1 person found this answer helpful.

  3. Hafeez Uddin 296 Reputation points
    2021-05-28T20:05:16.59+00:00

    You should add another step for failed notification in the same job.
    On failure, let the job execute the failed notification step and I believe ( not tested ) you can use script to read the failed step info.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.