Sie sind auf Seite 1von 8

Scheduling Jobs

from Chapter 12, Microsoft SQL Server 2000 Administrator's Pocket Consultant by William R. Stanek.

Job scheduling is a key part of database automation. You can configure SQL Server jobs to handle just
about any database task.

Creating Jobs
You create jobs as a series of steps that contain actions in the sequence in which you want to execute
them. When you schedule jobs in conjunction with other SQL Server facilities, such as database backups or
data transformation services (DTS), the necessary commands are configured for you. Normally these
commands are set as step 1, and all you need to do is set a run schedule for the job. You can add extra
steps to these jobs and thus perform other tasks. For example, after importing data through DTS, you may
want to back up the related database. In the DTS Wizard, you would schedule the import and then you
would edit the associated job in Enterprise Manager to add an additional step for backing up the database.
By coordinating the two processes, you ensure that the import operation is completed before starting the
backup.

Another reason for editing a job created by another SQL Server facility is to add notifications based on
success, failure, and completion of the job. In this way you can notify operators of certain conditions, and
you don't have to search through logs to determine whether the job executed properly.

When you schedule jobs to execute for alerts, you configure the entire job process from start to finish. You

Create a job definition

Set steps to execute

Configure a job schedule

Handle completion, success, and failure notification messages

Assigning or Changing Job Definitions


Whether you're creating a new job or editing an existing job, the steps for working with job definitions are
the same. Complete the following steps:
1.
2.
3.
4.
5.

In Enterprise Manager, access the Management folder on the server running SQL Server Agent.
Select the SQL Server Agent entry in the left pane and then double-click Jobs.
Existing jobs are shown in the right pane. Double-click a job to access its related properties dialog
box, which is essentially the same as the New Job Properties dialog box shown in Figure 12-7.
To create a new job, right-click the Jobs entry and, from the shortcut menu, choose New Job. This
displays the New Job Properties dialog box shown in Figure 12-7.
In the Name field, type a descriptive name for the job. The name can be up to 128 characters
long. If you change the name of an existing job, the job is displayed with the new name. Any

6.

references to the old job name in logs or history files remain the same and aren't modified.
If job scheduling across multiple servers is configured, select the target server. The target server
is the server on which the job runs. To run on the currently selected server, select Target Local Server.
To run on multiple servers, select Target Multiple Servers and then choose the target servers.

If your browser does not support inline frames, click here to view on a separate page.

Figure 12-7 Use the General tab of the New Job Properties dialog box to name, describe, and
categorize the job.
7.

Job categories help to organize jobs so they can be easily searched and differentiated. The default
category is Uncategorized (Local). Use the Category selection list to choose a different category for the
job.

Note Job categories are created and managed through a separate process. To create a new job
category or update an existing category, use the techniques described in the section of this chapter
entitled "Managing Job Categories."
8.

By default, the current user owns the job. Administrators can reassign jobs to other users. To do
this, use the Owner selection list. You can use only predefined logons. If the logon you want to use
isn't available, you'll need to create a logon for the account.

9.
10.

Type a description of the job in the Description text box. You can use up to 512 characters.
Click Apply to create the job definition. Then set Steps, Schedules, and Notifications as explained
in the following sections.

Setting Steps to Execute


Jobs can have one or more steps. While SQL Server Agent always attempts to execute the Start step,
additional steps can be executed conditionally, such as only when the Start step succeeds or fails. You work
with steps using the Steps tab in the New Job Properties dialog box, as shown in Figure 12-8. The dialog
box displays any existing steps for the job. You can use the fields and buttons in this dialog box as listed on
the following page.

If your browser does not support inline frames, click here to view on a separate page.

Figure 12-8 Using the Steps tab in the New Job Properties dialog box, you can execute steps
conditionally and in any sequence you specify.

New Creates a new step.

Insert Inserts a step before the currently selected step.

Edit Allows edits to the selected step.

Delete Deletes the selected step.

Move Step Up/Down Changes the order of the selected step.

Start Step Sets which step is executed first. The green flag icon highlights the start step in the
step list.

When you create or edit a step, you see a dialog box similar to the one shown in Figure 12-9. To configure
this dialog box, complete the following steps:
1.

Type a short but descriptive name for the step in the Step Name field.

2.

Use the Type selection list to choose a step type. Each step can

Execute Transact-SQL commands Type Transact-SQL commands in the Command area


or load the statements from a Transact SQL script. To load commands from a script, click Open
and then select the Transact-SQL script you want to use. The entire contents of the script are then
stored with this step.

Run ActiveX scripts You can write ActiveX scripts in VBScript, JScript, or another active
scripting language configured for use on the system. Enter script statements directly into the
Command area or load the statements from a script file. Again, the entire contents of the script
are then stored with this step, and later changes to the script file aren't updated automatically.

Execute operating system commands Enter the operating system commands on a


separate line, making sure that you specify the full path to commands and in command
parameters. Commands can run batch scripts, Windows scripts, command-line utilities, or
applications.

If your browser does not support inline frames, click here to view on a separate page.

Figure 12-9 Set summary information and commands to execute for the step you're
configuring using the fields in the General tab of the New Job Step dialog box.

Pass Transact-SQL commands to replication agents You can script the Distributor,
Snapshot, Merge, and Transaction LogReader agents with Transact-SQL commands. To see
examples, refer to the existing jobs that are configured to handle replication, distribution, and
subscription processes on the server (if available).

Tip Subsequent changes to scripts aren't updated automatically. You'll need to edit the step properties
and reload the script file. Additionally, you shouldn't edit existing replication jobs. Instead, modify the
replication process as described in Chapter 9, "Configuring Snapshot, Merge, and Transactional
Replication."

3.

When executing Transact-SQL commands, use the Database selection list to set the database on
which the commands are executed.

4.
5.

Click the Advanced tab as shown in Figure 12-10.


In the On Success Action selection list, set the action to take when the step succeeds. You can

Go to the next step to continue sequential execution of the job

Go to a different step to continue execution of the job on a different step

Quit the job and report success or failure

If your browser does not support inline frames, click here to view on a separate page.

Figure 12-10 Control the behavior and logging of the step using the fields in the Advanced
tab.
6.

By default, Retry Attempts is set to zero and SQL Server Agent doesn't try to execute steps again.
You can change this behavior by setting the number of retry attempts and a retry interval. You do this
by using the Retry Attempts and Retry Interval (Minutes) fields, respectively. The retry interval is the
delay in minutes between retries.

7.

If the job fails on all retry attempts (if any), the action set in the On Failure Action selection list is
executed. The available options are the same as those for success.
If desired, configure a file for logging output from Transact-SQL and CmdExec commands. Type
the filename and path in the Output File field or use the find file button () to search for an existing
file. An alternative is to append output to the step history.

8.

Tip You may want to create a central log file for the output of all jobs or only all jobs in a particular
category. If you do this, be sure to select the Append option button rather than the Overwrite option
button. This ensures that the output file doesn't get overwritten.
9.
10.

Set the login to use when executing commands using the Run As User selection list. By default,
commands are run using the current login ID.
Choose Apply to complete the step configuration.

Configuring Job Schedules


You track schedules in the Schedules tab of the New Job Properties dialog box, as shown in Figure 12-11.
Jobs can have one or more schedules associated with them, and just as you can enable or disable jobs and
their individual steps, you can enable or disable individual schedules. This makes the job scheduling
process very flexible. For example, you could set one schedule to execute the job on weekdays at 2 A.M.,
another to execute the job on Saturday and Sunday at 8 A.M., and another for ad hoc execution at 10 P.M.

If your browser does not support inline frames, click here to view on a separate page.

Figure 12-11 Track schedules in the Schedules tab of the New Job Properties dialog box. Jobs

can have multiple schedules associated with them.

Whether you're creating a new job or editing an existing job, you work with schedules in the Schedules tab
as follows:

Create a new schedule Click New Schedule to configure a new schedule.

Edit a schedule Select an existing schedule and then click Edit to view or modify its properties.

Delete a schedule Select an existing schedule and then click Delete to remove the schedule.

You create or edit schedules by completing the following steps:


1.

Click New Schedule to open the New Job Schedule dialog box, or click Edit to open the Edit Job
Schedule dialog box. These dialog boxes are essentially the same except for the title. Figure 12-12
shows the New Job Schedule dialog box.

2.

Type a name for the schedule and then select one of the following schedule types:

Start Automatically When SQL Server Agent Starts Runs the job automatically
whenever SQL Server Agent starts.

Start Whenever The CPU(s) Become Idle Runs the job whenever the CPU is idle. CPU
idle time is specified in the Advanced tab of the SQL Server Agent Properties dialog box.

If your browser does not support inline frames, click here to view on a separate page.

Figure 12-12 Use the New Job Schedule dialog box to set the schedule name and type.

One Time Runs the job once at the date and time specified in the On Date and At Time
fields.

3.
4.
5.

Recurring Runs the job according to the recurring schedule displayed.


Recurring jobs are the ones that need the most explanation. When you click Change, you'll see the
Edit Recurring Job Schedule dialog box shown in Figure 12-13. You can schedule recurring jobs to run
on a daily, weekly, or monthly basis.
To run the job on a daily basis, select the Daily option button. Then use the Every Day field to set
when the job runs. Daily recurring jobs can run every day, every other day, or every Nth day.
To run the job on a weekly basis, select the Weekly option button. Then configure the job using
these fields:

Every Nth Week(s) Allows you to run the task every week, every other week, or every
Nth week

o
6.

Day Of Week Sets the day(s) of the week when the task runs, such as on Monday or on

Monday, Wednesday, and Friday


To run the job on a monthly basis, select the Monthly option button. Then configure the job using
these fields:

Day N Of Every Nth Month Sets the day of the month and on which months the job

runs. For example, if you select Day 15 of every second month, the job runs on the 15th day of
alternating months.

The Nth Day Of Every Nth Month Sets the job to run on the Nth occurrence of a day in
a given month, such as the second Monday of every month or the third Sunday of every other
month.

If your browser does not support inline frames, click here to view on a separate page.

Figure 12-13 Configure a recurring schedule in the Edit Recurring Job Schedule dialog box.
7.

Set the Daily Frequency for the daily, weekly, or monthly job. You can configure jobs to run one or
more times on their scheduled run date. To run the job once on a given date, select Occurs Once At
and then set a time. To run the job several times on a given date, select Occurs Every and then set a
time interval in hours or minutes. Afterward, set a start and end time, such as from 7:30 A.M. to 5:30
P.M.

8.

By default, schedules begin on the current date and don't have a designated end date. To change
this behavior, select the End Date option button and then use the Start Date and End Date fields to set
a new duration for the schedule.
Click OK to close the Edit Recurring Job Schedule dialog box and then click OK again to close the
New Job Schedule dialog box, which completes the schedule process.

9.

Handling Notification Messages


Notification messages are generated when a job succeeds, fails, or completes. You can handle these
messages in several ways. You can notify operators, log the related event, automatically delete the job, or
do all three. To configure notification, complete the following steps:
1.
2.
3.
4.
5.

Access the Notifications tab of the job you want to configure. This tab is shown in Figure 12-14.
You can notify operators by e-mail, pager, or Net Send message. Select the check box for the
technique you want to use. Choose an operator to handle the notification. Then choose a notification
type. Repeat this process to configure other notification methods.
To log a particular type of notification message in the event log, select Write To Windows
Application Event Log and then select the notification type to log. Usually, you'll want to log failure, so
select When The Job Fails.
To delete a job upon notification, select Automatically Delete Job and then choose the notification
type that triggers the deletion.
Choose Apply.

Managing Existing Jobs


In Enterprise Manager, you manage jobs with the SQL Server Agent. To do that, complete the following
steps:
1.
2.
3.

Access the SQL Server Agent window in the console root. You'll see an entry labeled Jobs.
Select Jobs in the left pane and you'll see existing jobs in the right pane.
You can now double-click a job entry to access its related properties dialog box or right-click a job
entry to display a shortcut menu. Key options on the shortcut menu are

Delete Deletes the job definition. Before deleting a complex job, you may want to create a script

that can be used to recreate the job.

Disable Job Disables the job so it won't run.

Download If you've configured multiserver administration, use this option to post scheduled job
instructions to a target server. Downloading jobs to a target allows you to immediately start
execution of a job or to use the job on the target at a later date.

Script Job Select All Tasks and then choose Script Job to access the Generate SQL Script dialog
box. This dialog box enables you to generate a Transact-SQL script that you can use to recreate
the job.

Start Job Starts the selected job if it's not already running.

Stop Job Stops the selected job if it's running.

View Job History Displays the Job History dialog box. This dialog box enables you to view
summary or detail information on the job execution.

Managing Job Categories


You use job categories to organize jobs into topical folders. When you install SQL Server, default job
categories are created automatically. You can add new job categories and change the existing categories at
any time.

Working with Job Categories


To create a new job category or update an existing category, complete the following steps:
1.

In Enterprise Manager, access the SQL Server Agent window in the console root.

2.

Right-click the Jobs node. Select All Tasks, and then choose Manage Job Categories. This displays the
Job Categories dialog box.
You can delete a category by selecting it and then pressing Delete.
You can view the properties of a category by selecting it and then clicking Properties.
To add categories or to change the properties of a category, follow the steps outlined in the following
sections, "Creating Job Categories" or "Updating Job Categories," respectively.

3.
4.
5.

Creating Job Categories


You can create a new job category by completing the following steps:
1.

Access the Job Categories dialog box as explained previously. Click Add to display the New Job
Categories dialog box.

2.
3.

Type a name for the category in the Name field, and then select Show All Jobs.
All jobs defined on the current server should now be listed. Add a job to the new category by
selecting the corresponding check box in the Member column. Remove a job from the new category by
clearing the corresponding check box in the Member column.
Click OK when you're finished.

4.

Updating Job Categories


You can update an existing job category by completing the following steps:

1.

Access the Job Categories dialog box as explained previously. Click Properties to display a
properties dialog box.

2.
3.

Select Show All Jobs. All jobs defined on the current server should now be listed.
Add a job to a new category by selecting the corresponding check box in the Member column.
Remove a job from the category by clearing the corresponding check box in the Member column.

4.

Click OK when you're finished.

Das könnte Ihnen auch gefallen