Chapter 14 Working with SQL Server Agent Jobs Microsoft SQL Server features a powerful and flexible job-scheduling engine called SQL Server Agent. This chapter explains how you can use SQL Server Agent to define jobs and schedule them to automatically execute on a scheduled basis. You will see how to create maintenance plans to specify maintenance tasks such as backups to be performed against one or more databases. And this chapter shows you how to define operators to send messages about job success or failure and how to configure alerts, which enable you to monitor the system for specified conditions and execute jobs to proactively address potential issues. Exam objectives in this chapter: Implement and maintain SQL Server Agent jobs. Set a job owner. Create a job schedule. Create job steps. Configure job steps. Disable a job. Create a maintenance job. Set up alerts. Configure operators. Modify a job. Delete a job. Manage a job. Monitor SQL Server Agent job history. Identify the cause of a failure. Identify outcome details. Find out when a job last ran. 493
494 Chapter 14 Working with SQL Server Agent Jobs Lessons in this chapter: Lesson 1: Creating a SQL Server Agent Job............................. 495 Lesson 2: Creating a Maintenance Plan................................ 504 Lesson 3: Configuring Operators..................................... 515 Lesson 4: Configuring Alerts......................................... 519 Before You Begin To complete the lessons in this chapter, you must have SQL Server 2005 installed. A connection to a SQL Server 2005 instance in SQL Server Management Studio (SSMS). The AdventureWorks database installed. SQL Server Integration Services (SSIS) installed. Real World Michael Hotek As a database administrator (DBA), I need to perform many tasks on a recurring basis. The most common task is performing backups of my databases. All backups need to be done on a scheduled basis, such as running a transaction log backup every five minutes. Fortunately, SQL Server ships with a scheduling engine called SQL Server Agent. To meet my requirements of executing backups on a regularly scheduled basis, I create jobs that are then executed on the schedules that I define by using SQL Server Agent. Without having a scheduling engine in the database system, I would have to purchase third-party software to accomplish such management tasks. Executing backups on a scheduled basis is just one way to use SQL Server Agent to improve your productivity as a DBA and to ensure that important management tasks are performed when needed. Anything that you need to execute on a scheduled basis can take advantage of the services that this component offers.
Lesson 1: Creating a SQL Server Agent Job 495 Lesson 1: Creating a SQL Server Agent Job SQL Server Agent is the scheduling engine within SQL Server. One of the primary purposes of this engine is to execute defined jobs at specified intervals. You can define SQL Server Agent jobs to execute a variety of important tasks such as database backups, reindexing, and integrity checks. In this lesson, you will learn how to create a job in SQL Server Agent and how to configure the job options that are available. After this lesson, you will be able to: Create a job. Set a job owner. Create job steps. Create job schedules. Estimated lesson time: 20 minutes How to Create a SQL Server Agent Job A job in SQL Server Agent consists of job steps, an owner to provide the security context for the job, and one or more schedules for executing the job. The high-level steps for creating a new job are as follows: 1. Create a new job and give it a name, a database context, and an owner. 2. Add one or more job steps to the job. 3. Optionally specify a schedule on which to execute the job. To create a new job, you need to expand the SQL Server Agent node within the Object Explorer in SSMS, as shown in Figure 14-1. Right-click the Jobs node and choose New Job. The New Job window opens. In this window, you can define several general properties for each job, including name, job category, and description. A job name can be up to 64 characters long. Be sure to use a descriptive job name that clearly identifies the basic purpose of the job. You can use the job category to group jobs together based on the types of actions they perform. For example, you should specify Database Maintenance as the category for any jobs that execute maintenance tasks. You can use any of the built-in job categories that ship with SQL Server or you can create your own categories.
496 Chapter 14 Working with SQL Server Agent Jobs Figure 14-1 Managing all jobs, alerts, and operators from within the SQL Server Agent node A description text box enables you to enter additional details about a job. If specific business rules govern a job or the way it is constructed, you should specify them in the job description to facilitate future job-maintenance actions. You see the New Job window completed in Figure 14-2. Figure 14-2 Defining a new job from the New Job window
Lesson 1: Creating a SQL Server Agent Job 497 How to Specify a Job Owner On the New Job General page, you also specify the job owner, which defines the user or group who manages the job. Only the job owner or a member of the sysadmin role is allowed to modify a job. If the owner of the job is not a member of the sysadmin role, you need to ensure that the job owner has access to any proxy accounts necessary to execute a step within the job. You use SQL Server Agent proxy accounts to control fine-grained permissions to SQL Server Agent. These proxy accounts control access to certain external subsystems within SQL Server such as replication, SSIS, SQL Server Analysis Services, CmdExec, and ActiveX. When a job step requires the use of one of these subsystems, the job owner is validated for access to the proxy. Once validated, SQL Server Agent impersonates the proxy account to allow execution of the job step. MORE INFO Proxy accounts For more information about proxy accounts, see the SQL Server 2005 Books Online article How to: Configure a User to Create and Manage SQL Server Agent Jobs. SQL Server 2005 Books Online is installed as part of SQL Server 2005. Updates for SQL Server 2005 Books Online are available for download at www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx. For job steps that execute Transact-SQL statements, the security context for the job step is derived from the job owner. The SQL Server Agent proxies used, along with the job owner, prevent a user from gaining elevated permissions within SQL Server by using SQL Server Agent. Quick Check Under what security context does a job step run? Quick Check Answer A job step is executed using the security credentials of the job owner. For job steps that access external resources such as the file system, SSIS, and replication, a proxy account is used. How to Create Job Steps The core of a job is one or more job steps, which define the actual action(s) to be performed within the jobs.
498 Chapter 14 Working with SQL Server Agent Jobs The high-level steps for defining a job step are the following: 1. Create a new job step, specifying a name and type. 2. Define the command you want to execute. 3. Define logging and notification actions. To define a job step, select the Steps page within the New Job window, as shown in Figure 14-3. Figure 14-3 Defining a new job step After clicking New, you can define the job step s properties, as the example shows in Figure 14-4. For each job step, you need to define a name, which can be up to 64 characters. You also need to define a job step as a specific type. The most common type of job step is Transact-SQL. With this type of job step, you define the database context in which to run the Transact-SQL batch that you specify. You can define a simple batch of Transact-SQL statements to execute as the job step, but more often you specify a call to a stored procedure. Other types of job steps correspond to SSIS, Analysis Services, replication, and ActiveX calls.
Lesson 1: Creating a SQL Server Agent Job 499 Figure 14-4 Defining job step properties Depending on the type of job step you select, you have various configuration options available. You access the job step options by selecting the Advanced page in the New Job Step window, as Figure 14-5 shows. Figure 14-5 Specifying success, failure, and output file options
500 Chapter 14 Working with SQL Server Agent Jobs You can specify actions to take when the step completes successfully, such as Go To The Next Step, and actions to take when the step fails, such as Quit The Job, Reporting Failure. You can define how many times and at what interval to retry the job step. You can also specify logging options, such as writing to an output file. IMPORTANT Logging options DBAs rarely specify logging options for job steps. However, without logging, it is much more difficult to troubleshoot why a job step may be failing. You should always output to a file at a minimum. You can then scan the output file for error messages. How to Create Job Schedules After you create job steps for your SQL Server Agent job, you can attach one or more schedules to the job. Without a schedule, a job can be executed only on demand from another process. To create a new job schedule, select the Schedules page in the New Job window and then click New, as shown in Figure 14-6. Figure 14-6 Creating a job schedule On the resulting New Job Schedule dialog box, shown in Figure 14-7, you specify the schedule name, type, frequency, and duration. You can define job schedules to run on a periodic basis such as daily, weekly, or monthly with a variety of options available
Lesson 1: Creating a SQL Server Agent Job 501 depending on the base interval you choose. The most common job schedule frequency is to run the job daily, and within each day, every n hours or n minutes. Figure 14-7 Specifying scheduling options Instead of specifying a time interval for executing the job, you can use the Schedule Type drop-down list to schedule the job to execute based on CPU idle conditions, to execute when SQL Server Agent starts, or to execute only once. In addition, you can specify start and end dates for each schedule, allowing a particular job schedule to remain in effect only within that period. NOTE Job schedule differences in SQL Server 2005 In SQL Server 2000, job schedules are specific to a given job. In SQL Server 2005, however, job schedules are treated as separate entities. You can create a job schedule once and then use the same schedule for multiple jobs. Lesson Summary SQL Server Agent is a basic scheduling engine included with SQL Server 2005 that enables you to define jobs and the schedules on which to automatically execute them.
502 Chapter 14 Working with SQL Server Agent Jobs A job in SQL Server Agent consists of job steps: an owner to provide the security context for the job and one or more schedules for executing the job. To create a job step, you specify a name and type for the step, define the command to be executed in the step, define notification actions to occur on job step success or failure, and specify logging. You can define one or more job schedules to attach to a job, specifying the recurring interval for executing the job. Lesson Review The following questions are intended to reinforce key information presented in this lesson. The questions are also available on the companion CD if you prefer to review them in electronic form. NOTE Answers Answers to these questions and explanations of why each answer choice is right or wrong are located in the Answers section at the end of the book. 1. What is the role of a job owner? (Choose all that apply.) A. Categorize a job. B. Provide the security context for a job step. C. Execute the job. D. Control access to the job. 2. Which of the following are valid scheduling options? (Choose all that apply.) A. Once per day B. When CPU utilization exceeds 80 percent C. When SQL Server Agent starts D. Every five seconds PRACTICE Create a SQL Server Agent Job In this exercise, you will practice creating a SQL Server Agent job to back up the AdventureWorks database on a daily basis at 23:00. NOTE Naming conventions The exercises in this chapter do not explicitly specify the names to give entities. Each DBA has specific naming conventions, so all names should follow your standard naming conventions.
Lesson 1: Creating a SQL Server Agent Job 503 1. Open SSMS and connect to your SQL Server. 2. Expand the SQL Server Agent node. 3. Right-click Jobs and choose New Job. 4. Give the job a name and set a job owner with the authority to back up the AdventureWorks database. 5. Change the category to Database Maintenance. 6. Enter a description for the job. 7. Select the Steps page and click New to begin defining the backup job step. 8. Give the job step a name and specify Transact-SQL as the type. 9. Verify that the master database is selected in the Database drop-down list. NOTE Database context For exercises involving a database backup, this book specifies the master database as the database context because it will always exist on your SQL Server instance. However, there are no particular requirements to set a backup job to a specific database context. 10. Type the following command for the job step to run: BACKUP DATABASE AdventureWorks to DISK = '<dirpath>\adventureworks.bak' NOTE Backup file placement Replace <dirpath> with the directory in which you want to place the backup file, for example: m:\ program files\microsoft sql server\mssql.1\mssql\backup\adventureworks\fullbackup\20060201\ adventureworks.bak. 11. Click OK to create the job step. 12. Select the Schedules page and click New to define a new schedule to attach to the job. 13. Give the schedule a name. 14. Specify the following settings: Schedule Type: Recurring Occurs: Daily Recurs Every: One day Occurs Once At: 23:00, or 11:00 PM 15. Click OK to create the new job schedule. 16. Click OK to close the New Job window and create your new backup job.
504 Chapter 14 Working with SQL Server Agent Jobs Lesson 2: Creating a Maintenance Plan Maintenance plans in SQL Server 2005 have undergone a dramatic rearchitecture. All maintenance plans are now implemented as monolithic SSIS packages, and each package can have only one schedule attached to it. Thus, if you have multiple schedules that you want to attach to a maintenance plan, you will need to create one plan per schedule, which means that you will define many more plans than you had to in SQL Server 2000. Still, if you do not want to write code to perform your maintenance operations, a maintenance plan is a good starting point. Within a maintenance plan, you can specify tasks for full, differential, and transaction log backups as well as tasks for integrity checks and reindexing operations. MORE INFO Database backups For more information about database backups, see Chapter 11, Backing Up, Restoring, and Moving a Database. And for more information about integrity checks and reindexing, see Chapter 12, Using Transact-SQL to Manage Databases. After this lesson, you will be able to: Create a maintenance plan. Estimated lesson time: 20 minutes How to Create a Maintenance Plan The most straightforward way to create a maintenance plan is to use the SQL Server Maintenance Plan Wizard. The high-level steps that you take to define a maintenance plan within the wizard are the following: 1. Specify the target server. 2. Specify maintenance tasks to perform. 3. Define task properties. 4. Specify SSIS flow control. 5. Create a schedule. 6. Define logging options. To access the Maintenance Plan Wizard, open the Management node in SSMS, right-click Maintenance Plans, and choose Maintenance Plan Wizard, as shown in Figure 14-8.
Lesson 2: Creating a Maintenance Plan 505 Figure 14-8 Launching the Maintenance Plan Wizard After launching the Maintenance Plan Wizard for the first time, disable the introductory splash screen so that you go directly into the wizard. By clicking Next, you will access the target server definition screen (see Figure 14-9). On this screen, you give your maintenance plan a name and description, select the server on which you want to run the maintenance tasks, and specify how you want to authenticate to that server. Figure 14-9 Specifying target server and connection parameters
506 Chapter 14 Working with SQL Server Agent Jobs Click Next to display the Select Maintenance Tasks page, which enables you to select the check boxes for one or more maintenance tasks to perform, as shown in Figure 14-10. Table 14-1 lists the available maintenance tasks and their purposes. Figure 14-10 Specifying job tasks Table 14-1 Task Pointer Maintenance Tasks Purpose Changes the cursor to a point on the design surface Backup Database Task Check Database Integrity Task Execute SQL Server Agent Job Task Execute T-SQL Statement Task History Cleanup Task Maintenance Cleanup Task Performs a full, differential, or transaction log backup Checks database integrity Executes a SQL Server Agent job Executes the specified Transact-SQL statement Cleans up the job history Cleans up backups and reports
Lesson 2: Creating a Maintenance Plan 507 Table 14-1 Maintenance Tasks Task Notify Operator Task Rebuild Index Task Reorganize Index Task Shrink Database Task Update Statistics Task Purpose Sends a notification to an operator Rebuilds one or more indexes Reorganizes one or more indexes Shrinks a database Updates the statistics on one or more tables and indexes NOTE Each backup plan has only one task The most common tasks to perform in a maintenance plan are backups. You will never execute full, differential, and transaction log backups on the same schedule. Unfortunately, because you can specify only one schedule for the entire maintenance plan, each backup maintenance plan that you create will have only one task defined within it. Click Next to display the Select Maintenance Task Order page. Because a maintenance plan can contain multiple tasks, you can specify the execution order for these tasks, as shown in Figure 14-11. Figure 14-11 Specifying task order
508 Chapter 14 Working with SQL Server Agent Jobs NOTE Can t execute multiple tasks on same schedule Remember that when creating a maintenance plan, you are in essence creating an SSIS package, so you are defining the flow control definition within the SSIS package. However, you will not be executing multiple maintenance tasks on the same schedule, so this step in the wizard is superfluous. Depending on the maintenance task you select, the options that you can specify at this point in the wizard vary. If you select a maintenance plan for a database backup, you ll see the option to select the databases to apply the task to, as shown in Figure 14-12. Figure 14-12 Specifying databases to back up After you select the check box for the databases you want to back up, click OK. You can then specify the folder in which you want to store the backups, along with whether you want to verify the backup integrity, as Figure 14-13 shows. Click Next. When you have specified all the appropriate task options, you can define a schedule, as shown in Figure 14-14. To access scheduling options, click Change.
Lesson 2: Creating a Maintenance Plan 509 Figure 14-13 Specifying backup options Figure 14-14 Defining one schedule for the entire maintenance plan
510 Chapter 14 Working with SQL Server Agent Jobs The scheduling options available are the same as when you are scheduling a SQL Server Agent job (see Figure 14-15). The difference is that for a regular SQL Server Agent job, you can define multiple schedules for a regular job and reuse previously created job schedules, but you can define only one schedule for a maintenance plan. Figure 14-15 Specifying scheduling options Click OK to close the New Job Schedule dialog box. After defining the schedule, you see a summary of the scheduling options displayed within the Maintenance Plan Wizard, as Figure 14-16 shows. Click Next. For reporting purposes, you can configure each maintenance plan to write an output file or to e-mail the report about the actions it performs, as shown in Figure 14-17. You will usually specify writing to an output file that you can scan for errors. Click Next.
Lesson 2: Creating a Maintenance Plan 511 Figure 14-16 Single schedule definition for the maintenance plan Figure 14-17 Specifying reporting options At this point, you have completed the creation of a maintenance plan, and the wizard asks you to verify your choices, as Figure 14-18 shows. When you click Finish, the wizard performs the following steps: 1. Generates an SSIS package. 2. Stores that SSIS package within the msdb database.
512 Chapter 14 Working with SQL Server Agent Jobs 3. Creates a job in SQL Server Agent to execute the maintenance plan. 4. Creates entries in the sys.dbmaintplan* tables within the msdb database. Figure 14-18 Finishing maintenance plan creation Quick Check 1. What operations can a maintenance plan perform? 2. How many schedules can you apply to a maintenance plan? Quick Check Answers 1. A maintenance plan can back up databases, delete old backup files from the operating system, maintain indexes, execute another job, and shrink a database. 2. You can apply only one schedule to a maintenance plan. PRACTICE Create a Maintenance Job In this exercise, you will practice the creation of a maintenance plan to back up the AdventureWorks database. Instead of using the Maintenance Plan Wizard to create the maintenance plan, you will define it from a generic maintenance plan. 1. In SSMS, right-click the Maintenance Plans node in Object Explorer and choose New Maintenance Plan.
Lesson 2: Creating a Maintenance Plan 513 2. Specify a name for the maintenance plan and click OK. 3. Click the browser button ( ) to the right of the Schedule text box. 4. Configure the maintenance plan to run once per day at 23:00, or 11:00 PM. When you re done, click OK to close the Job Schedule Properties dialog box. 5. Click Connections to open the Manage Connections dialog box. Click Add and specify the server connection options. Click OK to save your changes and then click OK again to close the Manage Connections dialog box. 6. Click Logging and configure the logging options you want to use for this plan. 7. In the Maintenance Plan Tasks toolbox, drag and drop the Back Up Database Task onto the surface of the maintenance plan. 8. Double-click the Back Up Database Task and specify the AdventureWorks database to perform a full backup. Click OK to close the Back Up Database Task dialog box. 9. Drag and drop a Maintenance Cleanup Task onto the surface of the maintenance plan. 10. Select the Back Up Database Task. 11. Drag the green arrow from the Back Up Database Task to the Maintenance Cleanup Task, which creates a dependency between the two tasks so that the backup task runs first and the cleanup task runs second. 12. Double-click the Maintenance Cleanup Task and specify saving the backups for one day. When you re done, click OK to close the Maintenance Cleanup Task dialog box. 13. Click Save on the toolbar to save your maintenance plan. Lesson Summary A maintenance plan enables you to graphically configure SQL Server to perform one or more predefined maintenance tasks on a scheduled basis against one or more databases. The most common type of maintenance plan that you will create is to back up a database on a regular basis. Each maintenance plan is a monolithic SSIS package that can have only one schedule defined for it.
514 Chapter 14 Working with SQL Server Agent Jobs Lesson Review The following questions are intended to reinforce key information presented in this lesson. The questions are also available on the companion CD if you prefer to review them in electronic form. NOTE Answers Answers to these questions and explanations of why each answer choice is right or wrong are located in the Answers section at the end of the book. 1. What types of tasks can you define with the Maintenance Plan Wizard? (Choose all that apply.) A. Database creation B. Database backup C. Index rebuilding D. SSIS package execution
Lesson 3: Configuring Operators 515 Lesson 3: Configuring Operators The SQL Server Agent subsystem enables you to define operators to receive notifications about jobs. You can use this mechanism to enable SQL Server Agent to send a notice when a job has failed, for example, alerting DBAs so that they can quickly evaluate and repair problems. After this lesson, you will be able to: Configure operators. Estimated lesson time: 10 minutes How to Configure an Operator You configure an operator by naming it and specifying various notification methods for the operator and other parameters, such as an e-mail address to send the notification to. To begin the configuration, in SSMS, right-click the Operators node below SQL Server Agent, and choose New Operator to define properties for an operator, as Figure 14-19 shows. Figure 14-19 Defining operator parameters
516 Chapter 14 Working with SQL Server Agent Jobs Each operator needs a name. You then specify which notification methods e-mail, net send, and pager are valid for that operator. NOTE Enable the Messenger Service In order to use a net send, the Messenger Service must be enabled. You specify addresses or numbers for each valid notification method. In addition, you can configure an operator to receive notifications only during specific on-duty hours. NOTE Specifying a work week Unfortunately, the graphical user interface (GUI) assumes that the work week is Monday through Friday for everyone in the world. Selecting the Notifications page displays all jobs and alerts for which a particular operator is configured to receive notifications. Click OK to create the operator. Quick Check Which notification methods are valid for an operator? Quick Check Answer You can notify an operator using net send, e-mail, or text messaging. Managing and Troubleshooting Jobs You can manage SQL Server Agent jobs within SSMS. To access the list of jobs that are configured on an instance, expand the SQL Server Agent node within SSMS, right-click the Job Activity Monitor item, and choose View Job Activity. The Job Activity Monitor displays a list of the jobs for the instance, along with the date and time the job was last run and the next date and time the job will run. The Job Activity Monitor also displays whether the job is enabled and whether the last run was successful. You can enable, disable, start, and stop a job by rightclicking it within the job activity grid. The shortcut menu also enables you to access the detailed history for a particular job, including any error messages that might have occurred during job execution.
Lesson 3: Configuring Operators 517 PRACTICE Configuring Operators In this practice, you will configure an operator to receive e-mail notifications. 1. Expand the SQL Server Agent node. 2. Right-click Operators and choose New Operator. 3. Specify a name for the operator. 4. Specify an e-mail address for the operator. 5. Click OK. NOTE Sending e-mail To send e-mail to this operator, you must enable and configure Database Mail. Because text messaging also relies on the mail subsystem, it also requires you to configure Database Mail. For information about configuring Database Mail, see Lesson 2, Configuring Database Mail, in Chapter 2, Configuring SQL Server 2005. Lesson Summary Lesson Review SQL Server Agent enables you to define operators to receive notifications from jobs via e-mail, net send, or pager. Operators provide a convenient way to refer to one or more messaging addresses by a single name. To send e-mail or text messages, you need to have SQL Server Database Mail configured and enabled. The following questions are intended to reinforce key information presented in this lesson. The questions are also available on the companion CD if you prefer to review them in electronic form. NOTE Answers Answers to these questions and explanations of why each answer choice is right or wrong are located in the Answers section at the end of the book.
518 Chapter 14 Working with SQL Server Agent Jobs 1. Which notification methods are available for an operator? (Choose all that apply.) A. cell phone B. pager C. e-mail D. instant messaging
Lesson 4: Configuring Alerts 519 Lesson 4: Configuring Alerts The alert subsystem within SQL Server Agent enables you to notify administrators when specific conditions within an environment are met. SQL Server Agent polls the system on a periodic basis to check for any alert conditions. After this lesson, you will be able to: Configure alerts. Estimated lesson time: 10 minutes How to Configure Alerts The high-level steps for configuring alerts are the following: 1. Create a new alert. 2. Select a type of alert. 3. Configure conditions to monitor. 4. Define an action to be taken when a condition is met. 5. Define additional messaging options. You find SQL Server alerts in a node under SQL Server Agent. To configure a new alert, right-click the Alerts node and choose New Alert. All alerts should have a name associated with them that describes the purpose of the alert. You also define an alert as based on one of three types of events: performance condition, SQL Server event, or Windows Management Instrumentation (WMI) event. You base a performance-condition alert on a performance counter. You can specify only one performance counter for an alert, so if you need something more sophisticated that uses multiple counters, you cannot use a SQL Server alert. The example that Figure 14-20 shows specifies an alert condition when the percentage of the transaction log space used in the AdventureWorks database exceeds 80 percent. You base a SQL Server event alert on either an error code or a severity level, as shown in Figure 14-21. You can further refine the particular alert by restricting it to errors that contain a specific string within the error text.
520 Chapter 14 Working with SQL Server Agent Jobs Figure 14-20 Specifying alert parameters for a SQL Server performance condition Figure 14-21 Specifying alert parameters for a SQL Server event Figure 14-22 shows the final type of alert you can create: a WMI event. For this type of alert, you specify a predefined notification query for SQL Server Agent to use or enter your own custom notification query.
Lesson 4: Configuring Alerts 521 Figure 14-22 Specifying alert parameters for a WMI event When SQL Server detects that a condition specified by the alert is met, it raises the alert and executes a response based on the response configuration you define. The response to an alert can be as simple as notifying an operator by e-mail, pager, or the net send command. In some scenarios, you can configure SQL Server to respond by executing a job that contains a set of steps designed to address the particular alert event. If the response to an alert is to notify an operator, you can specify additional options for the notification, as Figure 14-23 shows.
522 Chapter 14 Working with SQL Server Agent Jobs Figure 14-23 Specifying alert options Quick Check What types of alerts can you define? Quick Check Answer You can define alerts for a single SQL Server performance counter, SQL Server error code, error severity level, or WMI event. PRACTICE Configure Alerts In this exercise, you will practice the creation of an alert to notify an operator when the AdventureWorks database has a fatal integrity error. 1. Open the SQL Server Agent node in SSMS. 2. Right-click Alerts and choose New Alert. 3. Enter a name for the alert. 4. In the Type drop-down list, verify that a SQL Server Event Alert is selected. 5. From the Database Name drop-down list, select the AdventureWorks database. 6. From the Severity drop-down list, select 023 Fatal Error: Database Integrity Suspect.
Lesson 4: Configuring Alerts 523 7. Select the Response page. 8. Select the Notify Operators check box. 9. In the Operator List, select an operator to notify by selecting the operator s E-Mail, Pager, or Net Send check boxes. 10. Select the Options page. 11. Select the E-Mail check box to configure the alert to include the error information in the e-mail it sends to operators. 12. Click OK to save the alert. Lesson Summary Lesson Review Alerts provide a basic monitoring capability within SQL Server. You can monitor SQL Server for alert conditions related to performance counters, error codes, error severity levels, or WMI events. When an alert is triggered, you can send a notification or configure the alert to execute a SQL Server Agent job that will resolve the situation. The following questions are intended to reinforce key information presented in this lesson. The questions are also available on the companion CD if you prefer to review them in electronic form. NOTE Answers Answers to these questions and explanations of why each answer choice is right or wrong are located in the Answers section at the end of the book. 1. What types of alerts can you define? (Choose all that apply.) A. Performance conditions for CPU utilization B. Performance conditions for SQL Server C. Security permissions being changed for a login D. Errors generated by an application
524 Chapter 14 Review Chapter Review To further practice and reinforce the skills you learned in this chapter, you can Review the chapter summary. Review the list of key terms introduced in this chapter. Complete the case scenario. This scenario sets up a real-world situation involving the topics of this chapter and asks you to create solutions. Complete the suggested practices. Take a practice test. Chapter Summary Key Terms SQL Server Agent provides a powerful and flexible scheduling engine within SQL Server. By using the capabilities exposed in SQL Server Agent, you can configure operations to automatically execute on a scheduled basis without requiring user intervention. You can use the Database Maintenance Plan Wizard to define maintenance tasks, such as database backups or reindexing tasks, to perform against one or more databases. You can configure operators to receive notifications such as job failure via e-mail, net send, or pager. By combining alerts with the job subsystem, you can use SQL Server Agent to monitor the system for conditions that you specify and then to execute jobs to proactively address potential issues. Do you know what these key terms mean? You can check your answers by looking up the terms in the glossary at the end of the book. alert job job schedule job step
Chapter 14 Review 525 maintenance job maintenance plan operator SQL Server Agent proxy Case Scenario: Scheduling Administrative Actions In the following case scenario, you will apply what you ve learned in this chapter. You can find answers to these questions in the Answers section at the end of this book. Contoso Limited, a health care company located in Bothell, WA, has a volatile database containing patient claims data. Contoso is under strict regulation and is required to protect all customer data within a database. Government regulations allow for minimal data loss in the event of a natural disaster. In the case of Contoso, minimal data loss is defined as no more than 10 minutes of data loss. In addition, Contoso needs to ensure that performance within its patient claims database is optimal. Each evening, Contoso receives data feeds from several external vendors that process payments to patients. Frequently, data in the feeds needs to be edited and reimported based on validation scripts that reconcile the data within the patient claims database with the data feeds submitted by the external vendors. While the import processes execute, no other transactions are issued against the patient claims database. The current process creates a full backup of the patient claims database at 23:00 before the import routines are executed. Contoso also executes a full database backup at 06:00. The exact time that this backup executes is not important as long as it executes before business operations begin for the day. At 04:00 each day, the system administrators at Contoso shut down the SQL Servers to perform routine maintenance such as applying service packs and hotfixes to the operating system and other software on the server. Contoso wants to improve its backup strategy to ensure that government regulations are met, as well as requiring a maximum of eight restore operations to recover a database.
526 Chapter 14 Review 1. How does Contoso guarantee that a full backup is created before the patient claims import routines are executed? 2. How do the Contoso DBAs guarantee that a full backup is performed before business operations start in the morning? 3. What backup strategy should be implemented to ensure that a database can be recovered with a maximum of eight restore operations? Suggested Practices To help you successfully master the exam objectives presented in this chapter, complete the following tasks. Create a SQL Server Agent job For this task, practice creating the following jobs: Practice 1 Create a job to perform a differential backup of the AdventureWorks database twice per day at 23:00 and 16:00. Practice 2 Create a job to perform a transaction log backup of the Adventure- Works database every 10 minutes. Create a Maintenance Plan For this task, practice creating the following maintenance plans: Create an Alert Practice 1 Using the Maintenance Plan Wizard, create a maintenance plan to perform a differential backup of the AdventureWorks database twice per day at 11:00 and 16:00. Practice 2 Using the Maintenance Plan Wizard, create a maintenance plan to perform a transaction log backup of the AdventureWorks database every 10 minutes. For this task, practice creating the following alert: Practice 1 Create an alert to notify an operator when there is a table-integrity error.
Chapter 14 Review 527 Take a Practice Test The practice tests on this book s companion CD offer many options. For example, you can test yourself on just the content covered in this chapter, or you can test yourself on all the 70-431 certification exam content. You can set up the test so that it closely simulates the experience of taking a certification exam, or you can set it up in study mode so that you can look at the correct answers and explanations after you answer each question. MORE INFO Practice tests For details about all the practice test options available, see the How to Use the Practice Tests section in this book s Introduction.