Multiple Commands Within a CmdExec Job Step

One irritating problem with the Operating System (CmdExec) SQL Job Type is that you can only run a single command. Most people get around this problem by adding another job step or two with additional commands, but this is not the best solution. You can also call a batch script, but when deploying to multiple databases I don’t like dragging batch scripts from one place to another.

This is more of a DOS command, but it makes life easy when creating an SQL Job Step that requires multiple commands:

SET var=filename.txt && del %var%

Keep in mind that && will evaluate the errorlevel (exit code) after running the first command and only continue to the second command if the first succeeds (errorlevel=0). And there are others:

A & B = execute command A, then execute command B (do not evaluate errorlevel).
A | B = execute command A, and redirect all output into the input of command B.
A && B = execute command A, and evaluate the errorlevel after running command A. If the exit code (errorlevel) is 0, execute command B.
A || B = execute command A, and evaluate the errorlevel after running command A. If the exit code (errorlevel) is 0, redirect all output into the input of command B.

Leave a Reply