Sets a schedule for a job.
Syntax
sp_attach_schedule { [ @job_id = ] job_id | [ @job_name = ] 'job_name' } , { [ @schedule_id = ] schedule_id | [ @schedule_name = ] 'schedule_name' }
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.
[ @job_name = ] 'job_name'
The name of the job to which the schedule is added. job_name is sysname, with a default of NULL.
Note: Either job_id or job_name must be specified, but both cannot 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.
[ @schedule_name = ] 'schedule_name'
The name of the schedule to set for the job. schedule_name is sysname, with a default of NULL.
Note: Either schedule_id or schedule_name must be specified, but both cannot be specified.
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.
Permissions
By default, members of the sysadmin fixed server role can execute this stored procedure.
Note that 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 cannot be deleted if the detach would leave it with no jobs, unless the caller is the schedule owner.
Lyftron 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 InvalidateInMemoryCache and the job RebuildCache.
Note: This example assumes that the job InvalidateInMemoryCache and job RebuildCache already exist.
EXEC sp_add_schedule @schedule_name = N'NightlyJobs' , @freq_type = 4, @freq_interval = 1, @active_start_time = 010000 ; EXEC sp_attach_schedule @job_name = N'InvalidateInMemoryCache', @schedule_name = N'NightlyJobs' ; EXEC sp_attach_schedule @job_name = N'RebuildCache', @schedule_name = N'NightlyJobs' ;