Skip to content

Latest commit

 

History

History
116 lines (77 loc) · 3.96 KB

sp-detach-schedule-transact-sql.md

File metadata and controls

116 lines (77 loc) · 3.96 KB
title description author ms.author ms.reviewer ms.date ms.service ms.subservice ms.topic f1_keywords helpviewer_keywords dev_langs
sp_detach_schedule (Transact-SQL)
sp_detach_schedule removes an association between a schedule and a job.
markingmyname
maghan
randolphwest
07/04/2024
sql
system-objects
reference
sp_detach_schedule
sp_detach_schedule_TSQL
sp_detach_schedule
TSQL

sp_detach_schedule (Transact-SQL)

[!INCLUDE SQL Server]

Removes an association between a schedule and a job.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

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

Arguments

[ @job_id = ] 'job_id'

The job identification number of the job to remove the schedule from. @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 remove the schedule from. @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 remove from 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 remove from 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.

[ @delete_unused_schedule = ] delete_unused_schedule

Specifies whether to delete unused job schedules. @delete_unused_schedule is bit, with a default of 0, which means that all schedules are kept, even if no jobs reference them. If set to 1, unused job schedules are deleted if no jobs reference them.

[ @automatic_post = ] automatic_post

[!INCLUDE ssinternalonly-md]

Return code values

0 (success) or 1 (failure).

Result set

None.

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 to determine whether the user owns the schedule. Only members of the sysadmin fixed server role can detach schedules from jobs owned by another user.

Examples

The following example removes an association between a NightlyJobs schedule and a BackupDatabase job.

USE msdb;
GO

EXEC dbo.sp_detach_schedule
    @job_name = 'BackupDatabase',
    @schedule_name = 'NightlyJobs';
GO

Related content