Job Object

后续版本的 Microsoft SQL Server 将删除该功能。请避免在新的开发工作中使用该功能,并着手修改当前还在使用该功能的应用程序。

The Job object exposes the attributes of a single SQL Server Agent job.

显示当前对象的 SQL-DMO 对象模型

注释

A SQL Server Agent job is a scheduled series of executable steps. Jobs are typically used to automate administrative tasks performed against a Microsoft SQL Server database. Jobs can contain multiple steps with branch logic based on the success or failure of any individual step. A SQL Server job can contain one or more schedules setting run times for the task defined by the steps of the job.

With the Job object, you can:

  • Create a SQL Server Agent job, setting the steps to perform and the scheduled run times.

  • Execute an existing job or interrupt job execution.

  • Enable and disable scheduled job execution.

  • Edit existing jobs by adding or removing job steps or changing times of execution.

  • Set the distribution properties of a job for servers participating in multiserver administration.

The Name property of a Job object uses the SQL Server data type sysname. The string must be unique for all jobs defined on a server running SQL Server.

Using SQL-DMO and the Job object, you must create a SQL Server Agent job before you can add job steps to it. A job must have at least one job step and a target server to be executable by SQL Server Agent.

After a SQL Server Agent job has at least one step and an execution target, you can use the Start method of the Job object to execute the job. To schedule the job for execution by SQL Server Agent, use the JobSchedule object.

To create a SQL Server Agent job

  1. Create a Job object.

  2. Set the Name property.

  3. Add the Job object to the Jobs collection of a connected JobServer object to create the SQL Server Agent job.

To complete the definition of a SQL Server Agent job

  1. Use the JobStep object to define a job step.

  2. Add the JobStep object to the JobSteps collection of the Job object.

  3. Set the StartStepID property of the Job object to the value of the StepID property of the JobStep added.

  4. Use the ApplyToTargetServer or ApplyToTargetServerGroup method of the Job object to set the execution target for the SQL Server Agent job. Use the string (local) to indicate the server on which the job is located.