Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to:
SQL Server
Azure SQL Managed Instance
Creates a schedule for a SQL Server Agent job.
Transact-SQL syntax conventions
Important
On Azure SQL Managed Instance, most, but not all SQL Server Agent features are currently supported. See Azure SQL Managed Instance T-SQL differences from SQL Server for details.
sp_add_jobschedule
[ [ @job_id = ] 'job_id' ]
[ , [ @job_name = ] N'job_name' ]
, [ @name = ] N'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 ]
[ , [ @schedule_id = ] schedule_id OUTPUT ]
[ , [ @automatic_post = ] automatic_post ]
[ , [ @schedule_uid = ] 'schedule_uid' OUTPUT ]
[ ; ]
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.
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.
Name of the schedule. @name is sysname, with no default.
Indicates the current status of the schedule. @enabled is tinyint, with a default of 1
(enabled). If 0
, the schedule isn't enabled. When the schedule is disabled, the job doesn't be run.
Value that indicates when the job is to be executed. @freq_type is int, and can be one of the following values:
Value | Description |
---|---|
1 |
Once |
4 |
Daily |
8 |
Weekly |
16 |
Monthly |
32 |
Monthly, relative to @freq_interval. |
64 |
Run when the SQL Server Agent service starts. |
128 |
Run when the computer is idle. |
Day that the job is executed. @freq_interval is int, with a default of 0
, and depends on the value of @freq_type as indicated in the following table:
Value of @freq_type | Effect on @freq_interval |
---|---|
1 (once) |
@freq_interval is unused. |
4 (daily) |
Every @freq_interval days. |
8 (weekly) |
@freq_interval is one or more of the following (combined with an OR logical operator):1 = Sunday2 = Monday4 = Tuesday8 = Wednesday16 = Thursday32 = Friday64 = Saturday |
16 (monthly) |
On the @freq_interval day of the month. |
32 (monthly relative) |
@freq_interval is one of the following:1 = Sunday2 = Monday3 = Tuesday4 = Wednesday5 = Thursday6 = Friday7 = Saturday8 = Day9 = Weekday10 = Weekend day |
64 (when the SQL Server Agent service starts) |
@freq_interval is unused. |
128 |
@freq_interval is unused. |
Specifies the units for @freq_subday_interval. @freq_subday_type is int, and can be one of these values:
Value | Description (unit) |
---|---|
1 |
At the specified time |
2 |
Seconds |
4 |
Minutes |
8 |
Hours |
Number of @freq_subday_type periods to occur between each execution of the job. @freq_subday_interval is int, with a default of 0
.
Further defines the @freq_interval when @freq_type is set to 32
(monthly relative).
@freq_relative_interval is int, and can be one of these values:
Value | Description (unit) |
---|---|
1 |
First |
2 |
Second |
4 |
Third |
8 |
Fourth |
16 |
Last |
@freq_relative_interval indicates the occurrence of the interval. For example, if @freq_relative_interval is set to 2
, @freq_type is set to 32
, and @freq_interval is set to 3
, the scheduled job would occur on the second Tuesday of each month.
Number of weeks or months between the scheduled execution of the job. @freq_recurrence_factor is int, with a default of 0
. @freq_recurrence_factor is used only if @freq_type is set to 8
, 16
, or 32
.
The date on which job execution can begin. @active_start_date is int, with a default of NULL
. The date is formatted as yyyyMMdd
. If @active_start_date is set, the date must be greater than or equal to 19900101
.
After the schedule is created, review the start date and confirm that it's the correct date. For more information, see the section "Scheduling Start Date" in Create and Attach Schedules to Jobs.
The date on which job execution can stop. @active_end_date is int, with a default of 99991231
. The date is formatted as yyyyMMdd
.
The time on any day between @active_start_date and @active_end_date to begin job execution. @active_start_time is int, with a default of 000000
. The time is formatted as HHmmss
on a 24-hour clock.
The time on any day between active_start_date and @active_end_date to end job execution. @active_end_time is int, with a default of 235959
. The time is formatted as HHmmss
on a 24-hour clock.
Schedule identification number assigned to the schedule if it's created successfully. @schedule_id is an OUTPUT parameter of type int.
Identified for informational purposes only. Not supported. Future compatibility is not guaranteed.
A unique identifier for the schedule. @schedule_uid is an OUTPUT parameter of type uniqueidentifier.
0
(success) or 1
(failure).
None.
Job schedules can now be managed independently of jobs. To add a schedule to a job, use sp_add_schedule
to create the schedule and sp_attach_schedule
to attach the schedule to a job.
You can grant EXECUTE
permissions on this procedure, but these permissions might 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:
For details about the permissions of these roles, see SQL Server Agent Fixed Database Roles.
The following example assigns a job schedule to SaturdayReports
, which executes every Saturday at 2:00 AM.
EXEC msdb.dbo.sp_add_jobschedule
@job_name = N'SaturdayReports', -- Job name
@name = N'Weekly_Sat_2AM', -- Schedule name
@freq_type = 8, -- Weekly
@freq_interval = 64, -- Saturday
@freq_recurrence_factor = 1, -- every week
@active_start_time = 20000 -- 2:00 AM
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register today