Beruflich Dokumente
Kultur Dokumente
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
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.
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.
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
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.
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.
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.
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
Whether you're creating a new job or editing an existing job, you work with schedules in the Schedules tab
as follows:
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.
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.
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
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.
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.
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
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.
View Job History Displays the Job History dialog box. This dialog box enables you to view
summary or detail information on the job execution.
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.
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.
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.