Skip to content

Latest commit

 

History

History
126 lines (86 loc) · 4 KB

sp-attach-schedule-transact-sql.md

File metadata and controls

126 lines (86 loc) · 4 KB
title description author ms.author ms.reviewer ms.date ms.service ms.subservice ms.topic f1_keywords helpviewer_keywords dev_langs
sp_attach_schedule (Transact-SQL)
sp_attach_schedule sets a schedule for a job.
markingmyname
maghan
randolphwest
03/04/2024
sql
system-objects
reference
sp_attach_schedule_TSQL
sp_attach_schedule
sp_attach_schedule
TSQL

sp_attach_schedule (Transact-SQL)

[!INCLUDE SQL Server]

Sets a schedule for a job.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: 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

[!INCLUDE msdb-execute-permissions]

Other users must be granted one of the following [!INCLUDE ssNoVersion] 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.

[!INCLUDE ssNoVersion] 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

Related content