sp_attach_schedule (Transact-SQL)

Applies to: SQL Server

Sets a schedule for a job.

Transact-SQL syntax conventions

Syntax

sp_attach_schedule
    [ [ @job_id = ] 'job_id' ]
    [ , [ @job_name = ] N'job_name' ]
    [ , [ @schedule_id = ] schedule_id ]
    [ , [ @schedule_name = ] N'schedule_name' ]
    [ , [ @automatic_post = ] automatic_post ]
[ ; ]

Arguments

[ @job_id = ] 'job_id'

The job identification number of the job to which the schedule is added. @job_id is uniqueidentifier, with a default of NULL.

Either @job_id or @job_name must be specified, but both can't be specified.

[ @job_name = ] N'job_name'

The name of the job to which the schedule is added. @job_name is sysname, with a default of NULL.

Either @job_id or @job_name must be specified, but both can't be specified.

[ @schedule_id = ] schedule_id

The schedule identification number of the schedule to set for the job. @schedule_id is int, with a default of NULL.

Either @schedule_id or @schedule_name must be specified, but both can't be specified.

[ @schedule_name = ] N'schedule_name'

The name of the schedule to set for the job. @schedule_name is sysname, with a default of NULL.

Either @schedule_id or @schedule_name must be specified, but both can't be specified.

[ @automatic_post = ] automatic_post

@automatic_post is bit, with a default of 1.

Remarks

The schedule and the job must have the same owner.

A schedule can be set for more than one job. A job can be run on more than one schedule.

This stored procedure must be run from the msdb database.

Permissions

This stored procedure is owned by the db_owner role. You can grant EXECUTE permissions for any user, but these permissions may be overridden during a SQL Server upgrade.

Other users must be granted one of the following SQL Server Agent fixed database roles in the msdb database:

  • SQLAgentUserRole
  • SQLAgentReaderRole
  • SQLAgentOperatorRole

The job owner can attach a job to a schedule and detach a job from a schedule without also having to be the schedule owner. However, a schedule can't be deleted if the detach would leave it with no jobs, unless the caller is the schedule owner.

For details about the permissions of these roles, see SQL Server Agent Fixed Database Roles.

SQL Server checks if the user owns both the job and the schedule.

Examples

The following example creates a schedule named NightlyJobs. Jobs that use this schedule execute every day when the time on the server is 01:00. The example attaches the schedule to the job BackupDatabase and the job RunReports.

Note

This example assumes that the job BackupDatabase and the job RunReports already exist.

USE msdb;
GO

EXEC sp_add_schedule
    @schedule_name = N'NightlyJobs' ,
    @freq_type = 4,
    @freq_interval = 1,
    @active_start_time = 010000 ;
GO

EXEC sp_attach_schedule
   @job_name = N'BackupDatabase',
   @schedule_name = N'NightlyJobs' ;
GO

EXEC sp_attach_schedule
   @job_name = N'RunReports',
   @schedule_name = N'NightlyJobs' ;
GO