Dela via


Job Object

This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.

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

SQL-DMO object model that shows the current object

Remarks

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.