SQL Server Agent Job – Using Parameter to Determine Job Flow

parametersql serversql-server-agent

Is it possible to have an Agent Job with an input parameter?

What I'm looking to do is have a flag to determine how the job flow will occur.
For example: I have a 10 step job. Steps 1-5 are always going to be executed. However, if the flag is set to 1, then then next step is going to be 6, and if the flag is not set, then execute step 8.

Can anyone please suggest how I could go about doing this?
Thanks.

Best Answer

Expanding a bit on what @Nic said in his comment. Create a quick table in an applicable database.

CREATE TABLE JobParam AS (Param1 varchar(50))

Then set it appropriately before you start your job.

UPDATE JobParam SET Param1 = 'JobPath1'

As Nic suggested I would put some code at the top of steps 6+ like such.

IF (SELECT 1 FROM JobParam WHERE Param1 = 'JobPath1')
BEGIN
    T-SQL statements
END

Another option is to put code in the job that causes a failure if the param has a specific value and use the fail/succeed logic of the job steps to branch your job.

Either way I would make sure to put in a step at the end that always runs and re-sets your value to a default in case the job is run but someone forgot to update the parameter. Maybe have the default bypass the entire job.

UPDATE JobParam SET Param1 = 'FailJob'