Schedule a JobSchedule a Job

适用于: 是SQL Server 是Azure SQL 数据库(仅限托管实例)否Azure Synapse Analytics (SQL DW) 否并行数据仓库 APPLIES TO: YesSQL Server YesAzure SQL Database (Managed Instance only) NoAzure Synapse Analytics (SQL DW) NoParallel Data Warehouse

重要

Azure SQL 数据库托管实例目前支持大多数但并非所有 SQL Server 代理功能。On Azure SQL Database Managed Instance, most, but not all SQL Server Agent features are currently supported. 有关详细信息,请参阅 Azure SQL 数据库托管实例与 SQL Server 之间的 T-SQL 差异See Azure SQL Database Managed Instance T-SQL differences from SQL Server for details.

本主题介绍如何安排 SQL ServerSQL Server 代理作业计划。This topic describes how to schedule a SQL ServerSQL Server Agent job.

开始之前Before You Begin

安全性Security

有关详细信息,请参阅实现 SQL Server 代理安全性For detailed information, see Implement SQL Server Agent Security.

使用 SQL Server Management StudioUsing SQL Server Management Studio

创建计划并将其附加到作业中To create and attach a schedule to a job

  1. “对象资源管理器” 中,连接到 SQL Server 数据库引擎SQL Server Database Engine的实例,然后展开该实例。In Object Explorer, connect to an instance of the SQL Server 数据库引擎SQL Server Database Engine, and then expand that instance.

  2. 展开“SQL Server 代理” ,展开“作业” ,右键单击要计划的作业,并单击“属性” 。Expand SQL Server Agent, expand Jobs, right-click the job you want to schedule, and click Properties.

  3. 选择 “计划” 页,再单击 “新建”Select the Schedules page, and then click New.

  4. “名称” 框中,键入新计划的名称。In the Name box, type a name for the new schedule.

  5. 如果不希望计划在创建后立即生效,则清除 “启用” 复选框。Clear the Enabled check box if you do not want the schedule to take effect immediately following its creation.

  6. 对于 “计划类型” ,请选择下列操作之一:For Schedule Type, select one of the following:

    • 单击 “SQL Server 代理启动时自动启动” ,在启动 SQL ServerSQL Server Agent 服务时启动作业。Click Start automatically when SQL Server Agent starts to start the job when the SQL ServerSQL Server Agent service is started.

    • 单击 “CPU 空闲时启动” ,在 CPU 达到空闲条件时启动作业。Click Start whenever the CPUs become idle to start the job when the CPUs reach an idle condition.

    • 如果希望反复运行计划,则单击 “重复执行”Click Recurring if you want a schedule to run repeatedly. 若要设置重复执行的计划,请完成对话框上的 “频率”“每天频率”“持续时间” 组。To set the recurring schedule, complete the Frequency, Daily Frequency, and Duration groups on the dialog.

    • 如果希望仅运行一次计划,请单击 “执行一次”Click One time if you want the schedule to run only once. 若要设置“执行一次” 计划,请完成对话框上的“执行一次” 组。To set the One time schedule, complete the One-time occurrence group on the dialog.

将计划附加到作业中To attach a schedule to a job

  1. “对象资源管理器” 中,连接到 SQL Server 数据库引擎SQL Server Database Engine的实例,然后展开该实例。In Object Explorer, connect to an instance of the SQL Server 数据库引擎SQL Server Database Engine, and then expand that instance.

  2. 展开“SQL Server 代理” ,展开“作业” ,右键单击要计划的作业,然后单击“属性” 。Expand SQL Server Agent, expand Jobs, right-click the job that you want to schedule, and click Properties.

  3. 选择 “计划” 页,再单击 “选取”Select the Schedules page, and then click Pick.

  4. 选择要附加的计划,然后单击 “确定”Select the schedule that you want to attach, and then click OK.

  5. 在“作业属性” 对话框中,双击附加的计划。In the Job Properties dialog box, double-click the attached schedule.

  6. 验证是否正确设置了 “开始日期”Verify that Start date is set correctly. 如果该选项的设置不正确,则将日期设置为要让计划启动的日期,然后单击 “确定”If it is not, set the date when you want for the schedule to start, and then click OK.

  7. “作业属性” 对话框中,单击 “确定”In the Job Properties dialog box, click OK.

使用 Transact-SQLUsing Transact-SQL

安排作业计划To schedule a job

  1. “对象资源管理器” 中,连接到 数据库引擎Database Engine的实例。In Object Explorer, connect to an instance of 数据库引擎Database Engine.

  2. 在标准菜单栏上,单击 “新建查询”On the Standard bar, click New Query.

  3. 将以下示例复制并粘贴到查询窗口中,然后单击“执行” 。Copy and paste the following example into the query window and click Execute.

    USE msdb ;  
    GO  
    -- creates a schedule named NightlyJobs.   
    -- Jobs that use this schedule execute every day when the time on the server is 01:00.   
    EXEC sp_add_schedule  
        @schedule_name = N'NightlyJobs' ,  
        @freq_type = 4,  
        @freq_interval = 1,  
        @active_start_time = 010000 ;  
    GO  
    -- attaches the schedule to the job BackupDatabase  
    EXEC sp_attach_schedule  
       @job_name = N'BackupDatabase',  
       @schedule_name = N'NightlyJobs' ;  
    GO  
    

有关详细信息,请参阅 sp_add_schedule (Transact-SQL)sp_attach_schedule (Transact-SQL)For more information, see sp_add_schedule (Transact-SQL) and sp_attach_schedule (Transact-SQL).

使用 SQL Server 管理对象Using SQL Server Management Objects

通过使用所选编程语言(如 Visual Basic、Visual C# 或 PowerShell)来使用 JobSchedule 类。Use the JobSchedule class by using a programming language that you choose, such as Visual Basic, Visual C#, or PowerShell. 有关详细信息,请参阅SQL Server 管理对象 (SMO)For more information, seeSQL Server Management Objects (SMO).