The Failed Job

Wayne Sheffield, 2013-11-15 (first published: 2013-11-07)

I was looking into a failed job on a SQL Server 2008R2 instance. My first step was to check the history of the job, to see why it failed:

The Failed Job - History

Okay, this is interesting. The job did fail, but the only step to run succeeded. Hmm, let me look at what the job is doing:

The Failed Job - Steps

The job starts with step 1, and continues through all of the steps. In this example, each step just does a “SELECT 1″.

So, why is this job only running the first step, and why is it failing? Well, after a bit of head scratching, I decided to script out the job so that I could drop and recreate it. When I examined the script, I was able to find out what the problem was. The first step has this code:

Step 1 code    
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Step 1',
        @os_run_priority=0, @subsystem=N'TSQL',
        @command=N'SELECT 1',

Do you see what the issue is? The on success action is set to 4. If we look this up in BOL, we see that 4 is used to run a specified step. And the step to run is step 5 – a step doesn’t exist.

At this point, just saving the job from the GUI will fix the issue, since the step is showing in the GUI to run the next step. After doing this, scripting out the job again will show that the success action is 3 (which is to run the next step) and the step id is 0.

So thinking that I had found a bug, I decided to see if this still happens in newer versions of SQL. In SQL Server 2012, we have a difference:

The Failed Job - History 2012

Here we have a much better message as for why the job failed. It’s much better because it actually is the right reason for why the job did fail.

However, in SQL 2012, SSMS still shows step 1 as set to “Go to the next step”:

The Failed Job - Steps 2012

What about SQL 2014? In SQL 2014 CTP2, it’s the same as in SQL 2012.

I feel that the GUI should not show as “Go to the next step” if that is not what it is set as. Whether it says to just “Go to step (5)”, or “Go to <invalid step>” doesn’t matter, what does matter is that the incorrect action for the step is being shown. Without scripting out the job, you would never see this. So I have opened up a connect item for fixing the GUI. Please go out and vote for it. (If you want to try this out yourself, a script is attached to the connect item that will create a job with this issue.)

Note that to set up a job in this way, it needs to be done through a script. And when you do create the job, it does give you a warning about a non-existent job step. However, you also get that warning if you set up step 1 to go to step 3, because at the time that step 1 is created, the specified step doesn’t exist. Yes, the person running the script should investigate further – but the tool for doing so makes it look like everything is okay, and in fact it is showing wrong information. If you are being brought in to troubleshoot the failed job, your job just became a bit harder since the GUI isn’t accurate. If you were to just click OK to save the job, it would update this job to go to the next step – but what if the job is actually missing a step that it should be going to instead? So, if you run into this situation, slow down and ensure that you are configuring the job correctly.








Related content

Database Mirroring FAQ: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Question: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? This question was sent to me via email. My reply follows. Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? Databases to be mirrored are currently running on 2005 SQL instances but will be upgraded to 2008 SQL in the near future.

Robert Davis


1,567 reads

Networking – Part 4

You may want to read Part 1 , Part 2 , and Part 3 before continuing. This time around I’d like to talk about social networking. We’ll start with social networking. Facebook, MySpace, and Twitter are all good examples of using technology to let…

Andy Warren


1,530 reads

Speaking at Community Events – More Thoughts

Last week I posted Speaking at Community Events – Time to Raise the Bar?, a first cut at talking about to what degree we should require experience for speakers at events like SQLSaturday as well as when it might be appropriate to add additional focus/limitations on the presentations that are accepted. I’ve got a few more thoughts on the topic this week, and I look forward to your comments.

Andy Warren


360 reads