sp_add_schedule (Transact-SQL)
Creates a schedule that can be used by any number of jobs.
sp_add_schedule [ @schedule_name = ] 'schedule_name'
[ , [ @enabled = ] enabled ]
[ , [ @freq_type = ] freq_type ]
[ , [ @freq_interval = ] freq_interval ]
[ , [ @freq_subday_type = ] freq_subday_type ]
[ , [ @freq_subday_interval = ] freq_subday_interval ]
[ , [ @freq_relative_interval = ] freq_relative_interval ]
[ , [ @freq_recurrence_factor = ] freq_recurrence_factor ]
[ , [ @active_start_date = ] active_start_date ]
[ , [ @active_end_date = ] active_end_date ]
[ , [ @active_start_time = ] active_start_time ]
[ , [ @active_end_time = ] active_end_time ]
[ , [ @owner_login_name = ] 'owner_login_name' ]
[ , [ @schedule_uid = ] schedule_uid OUTPUT ]
[ , [ @schedule_id = ] schedule_id OUTPUT ]
[ , [ @originating_server = ] server_name ] /* internal */
By default, members of the sysadmin fixed server role can execute this stored procedure. Other users must be granted one of the following SQL Server Agent fixed database roles in the msdb database:
SQLAgentUserRole
SQLAgentReaderRole
SQLAgentOperatorRole
For details about the permissions of these roles, see SQL Server Agent Fixed Database Roles.
A. Creating a schedule
The following example creates a schedule named RunOnce. The schedule runs one time, at 23:30 on the day that the schedule is created.
USE msdb ;
GO
EXEC dbo.sp_add_schedule
@schedule_name = N'RunOnce',
@freq_type = 1,
@active_start_time = 233000 ;
GO
B. Creating a schedule, attaching the schedule to multiple jobs
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