Adds a step (operation) to a job.
sp_add_jobstep [@job_id =] job_id | [@job_name =] 'job_name']
[,[@step_id =] step_id]
{,[@step_name =] 'step_name'}
[,[@subsystem =] 'subsystem']
[,[@command =] 'command']
[,[@additional_parameters =] 'parameters']
[,[@cmdexec_success_code =] code]
[,[@on_success_action =] success_action]
[,[@on_success_step_id =] success_step_id]
[,[@on_fail_action =] fail_action]
[,[@on_fail_step_id =] fail_step_id]
[,[@server =] 'server']
[,[@database_name =] 'database']
[,[@database_user_name =] 'user']
[,[@retry_attempts =] retry_attempts]
[,[@retry_interval =] retry_interval]
[,[@os_run_priority =] run_priority]
[,[@output_file_name =] 'file_name']
[,[@flags =] flags]
Note Either job_id or job_name must be specified, but both cannot be specified.
Value | Description |
---|---|
‘ACTIVESCRIPTING’ | Active Script |
‘CMDEXEC’ | Operating-system command or executable program |
‘DISTRIBUTION’ | Replication Distribution Agent job |
‘SNAPSHOT’ | Replication Snapshot Agent job |
‘LOGREADER’ | Replication Log Reader Agent job |
‘MERGE’ | Replication Merge Agent job |
‘TSQL’ (default) | Transact-SQL statement |
Value | Description |
---|---|
[A-DBN] | Database name. If the job is run by an alert, this token automatically replaces the version 6.5 [DBN] token during the conversion process. |
[A-SVR] | Server name. If the job is run by an alert, this token automatically replaces the version 6.5 [SVR] token during the conversion process. |
[A-ERR] | Error number. If this job is run by an alert, this token automatically replaces the version 6.5 [ERR] token during the conversion process. |
[A-SEV] | Error severity. If the job is run by an alert, this token automatically replaces the version 6.5 [SEV] token during the conversion process. |
[A-MSG] | Message text. If the job is run by an alert, this token automatically replaces the version 6.5 [MSG] token during the conversion process. |
[DATE] | Current date (in YYYYMMDD format). |
[JOBID] | Job ID. |
[MACH] | Computer name. |
[MSSA] | Master SQLServerAgent name. |
[SQLDIR] | The directory in which SQL Server is installed. By default, this value is C:\Mssql7. |
[STEPCT] | A count of the number of times this step has executed (excluding retires). Can be used by the step command to force termination of a multistep loop. |
[STEPID] | Step ID. |
[TIME] | Current time (in HHMMSS format). |
[STRTTM] | The time (in HHMMSS format) that the job began executing. |
[STRTDT] | The date (in YYYYMMDD format) that the job began executing. |
Value | Description (action) |
---|---|
1 (default) | Quit with success. |
2 | Quit with failure. |
3 | Go to next step. |
4 | Go to step on_success_step_id. |
Value | Description (action) |
---|---|
1 | Quit with success. |
2 (default) | Quit with failure. |
3 | Go to next step. |
4 | Go to step on_fail_step_id. |
Value | Description |
---|---|
2 | Append to output file. |
4 | Overwrite output file. |
0 (default) | No options set. |
0 (success) or 1 (failure)
None
SQL Server Enterprise Manager provides an easy, graphical way to manage jobs, and is the recommended way to create and manage the job infrastructure.
Execute permissions default to the public role.
This example creates a job step that changes database access to read-only for a database named sales. In addition, this example specifies five retry attempts every 5 minutes.
Note This example assumes that the Weekly Sales Data Backup job already exists.
USE msdb
EXEC sp_add_jobstep @job_name = 'Weekly Sales Data Backup',
@step_name = 'Set database to read only',
@subsystem = 'TSQL',
@command = 'exec sp_dboption ''sales'', ''read only'', ''true''',
@retry_attempts = 5,
@retry_interval = 5
Modifying and Viewing Jobs | sp_help_job |
sp_add_job | sp_help_jobstep |
sp_add_jobschedule | sp_update_jobstep |
sp_delete_jobstep | System Stored Procedures |