Sql-server – Advanced job flow control

jobssql serversql server 2014

I want to create a job with two steps:
1. Success – go to step two, failure – exit(f).
2. Success – exit(s), failure – go to step one, but if this step fails more than once – exit(f).
Initial thought was to query some kind of job run id in step one, and if there are four (or more) rows for this id exit with failure, but there is no such thing as run id.
What is the best way to implement this logic?

Best Answer

You can create duplicates of steps 1 and 2 as steps 3 and 4, and set the following flow:

  1. Success - go to step two, failure - exit(f).
  2. Success - exit(s), failure - go to step three
  3. Success - go to step four, failure - exit(f).
  4. Success - exit(s), failure - exit(f).

But if you can instead wrap steps 1 and 2 in a stored procedure or ad hoc query that handles the retry logic without duplication, then your solution will be much more understandable and maintenance-safe.