SQL Server – Conditional Execution for SQL Server Agent Job Steps

sql serversql-server-agent

Is conditional execution possible for SQL Server Agent job steps?

That is if Step 1 returns 1 I want to go to Step 2.

If Step 1 returns 2 I want to go to Step 3.

If Step 1 returns 3 I want to go to Step 4.

It looks like it is not possible because the only distinction available for step completion is Success or Failure but I figured I would check with the experts here.

I know I can achieve this logic in SSIS package but was wondering if it can be done within SQL Server Agent.

Best Answer

You can't configure the agent to act as you have described, but you might be able to achieve the desired result by building the logic into the code executed in steps 2, 3, and 4. For instance you could use an IF statement at the start of step 2 to check the result of step 1, and only continue if the result was 1. If the result is not 1, then it would move on to step 3, and so on.