SQL Server allows you to respond to potential problems by creating alerts to respond to SQL Server errors, user-defined errors, or SQL Server performance conditions. You can also create a fail-safe operator in the event that a pager notification fails to contact an operator. This lesson describes how to perform these tasks.
After this lesson, you will be able to
- Create alerts to respond to SQL Server 7 errors
- Use SQL Server performance condition alerts to notify a system or database administrator (also known as an operator) of potential problems before they occur
- Configure SQL Server event forwarding
- Assign a fail-safe operator
Estimated lesson time: 90 minutes
Alerts respond to SQL Server or user-defined errors (events) that have been written to the Windows NT application log. SQL Server errors are raised in response to predefined problems, such as insufficient user permissions to modify a table or the transaction log becoming full. To raise user-defined messages, the database application (typically a stored procedure or trigger) must call the RAISERROR statement.
In this exercise, you will use the Create Alert Wizard to create an alert that is defined to send e-mail and net send notifications when an error with severity level 17 occurs.
Option | Value |
---|---|
For any error of severity | 017 — Insufficient Resources |
Database name | (All Databases) |
Job to execute | (No job) |
Notify operator(s) | Admin — E-mail and Net send |
Include error message text in | E-mail and Net send |
Alert notification message to send to operator | My first alert |
Alert name | Severity level 17 errors |
What additional responses to the alert can be defined that were not included as part of the wizard?
Answer
You can create alerts that fire in response to specific SQL Server errors or that fire in response to all errors of a specific severity level.
TIP
SQL Server has a number of predefined demonstration alerts. Since there are no operators assigned to these alerts, they do nothing. You should assign operators to these alerts and give them names that are more meaningful or replace them with alerts of your own.
You can use SQL Server Enterprise Manager, the Create Alert Wizard, or the sp_add_alert system stored procedure to define a new alert. The alert definition is stored in the msdb..sysalerts system table. This table is maintained in cache to improve performance. When you define an alert for a SQL Server error number, consider the following facts and guidelines:
For example, to respond to error number 9002 in both the payroll and customer databases, you could create two separate alerts. Alternatively, you could create one alert to respond to error number 9002 in all databases.
For example, if you create an alert for error 18456, the error message includes the name of a user who fails to log on. You could specify the text "Login failed for user 'sa'" so that the alert occurs only when someone is trying to log on as sa.
The following table provides a summary of the SQL Server error severity levels.
Severity level | Description | Can be corrected by | Written to NT application log |
---|---|---|---|
0 - 10 | Informational messages; these are not errors | N/A | Optional |
11 - 16 | User errors | User | Optional |
17 | Insufficient resource errors | Administrator, possibly database owner | Optional |
18 | Nonfatal internal error | Administrator | Optional |
19 | Nonconfigurable resource error | Administrator | Yes |
20 - 25 | Fatal errors | Administrator | Yes |
When you define an alert for an error severity level as a condition, consider the following facts and guidelines:
You can create alerts based on user-defined (customized) error messages. These can be defined for individual database applications, allowing you to define sophisticated solutions to avoid potential problems before they occur.
For example, you could create a user-defined error message to be raised from an update trigger on an inventory table. When a column in the inventory table is updated, indicating that inventory levels have fallen below 25 percent for a particular product, the trigger will raise your user-defined error. You could then define an alert for the error message that executes a job to reorder inventory and sends an e-mail message to the purchasing agent.
Before creating an alert for a user-defined error message, you must first create the error message. The programmer must raise the error from the database application, using the RAISERROR statement; SQL Server will not raise the error.
To create user-defined error messages, you can use SQL Server Enterprise Manager or the sp_addmessage system stored procedure. When you create user-defined error messages, consider the following facts:
An account manager wants to be notified by e-mail any time a customer is removed from the database. She also wants to know the name of the employee who deleted the customer in the event that subsequent action is necessary.
The following steps give the sequence of events that occur in this scenario when the alert fires. Figure 13.7 illustrates the scenario.
Figure 13.7 Example of an alert based on a user-defined error message
In this exercise, you will use SQL Server Enterprise Manager to create a user-defined error message that occurs when the number of stock units for a particular product reaches the reorder level.
NOTE
The Messages tab of the Manage SQL Server Messages dialog box will always be empty when you display it. If you want to see messages, you must first enter search criteria on the Search tab and click Find. This will populate the Messages tab with a list of messages that match your search criteria. For this exercise, you are adding a message, so it is not necessary to enter search criteria.
The units in stock for %s has reached %d. Please reorder.
In this exercise, you will create an alert that sends an e-mail message to the warehouse manager when your new user-defined error message occurs.
Note that as you type, the message next to Error Number changes from (Not a valid error number) to the error message when the error number is found in the sysmessages table.
You can search for errors by message text, error number, or severity by clicking browse (…) to open the Manage SQL Server Messages dialog box.
In this exercise, you will use SQL Server Query Analyzer to execute a stored procedure in the StudyNwind database that will raise error 50001 to test that the Reorder Inventory alert works as expected.
USE StudyNwind EXEC reorder @prodid = 2 |
Did you receive the defined notifications in response to the alert?
Answer
You can configure SQL Server Agent to forward unhandled event messages or all event messages to another SQL Server. You can specify that only events above a certain severity level be forwarded. The other SQL Server handles the events based on its own alert definitions. The name of the server on which the error occurred will be reported in the alert notification.
In a multiserver environment, this means that you need to define alerts on only one server. You may want to forward events to a server that has less traffic than other servers.
For example, you can configure errors with severity levels of 18 or above to be forwarded to the Accounting server, as shown in Figure 13.8. If an error occurs on your server with severity level 19, the event is automatically forwarded to the Accounting server to address the problem.
Figure 13.8 The Advanced tab of the SQL Server Agent Properties dialog box, showing the SQL Server Event Forwarding options
IMPORTANT
Event forwarding is available only when SQL Server is installed on Windows NT, not Windows 95 or Windows 98.
You can create alerts to respond to SQL Server performance conditions defined by the objects and counters used in Windows NT Performance Monitor. An alert is fired when the monitored value exceeds, equals, or falls below a defined limit. Such alerts allow you to proactively avert problems and keep your server and databases running smoothly.
For example, you can create a performance condition alert to be fired when the transaction log in the Northwind database has exceeded 75 percent of its capacity. Figure 13.9 illustrates such an alert.
Figure 13.9 Example of an alert based on a performance condition
You can create SQL Server performance condition alerts based on most of the SQL Server Performance Monitor objects. Examples of the measures on which you can base alerts include
Windows NT Performance Monitor does not need to be running on your SQL Server for you to use performance condition alerts.
NOTE
Previous versions of SQL Server required Windows NT Performance Monitor to be running in order to use performance-based alerts. A utility called sqlalrtr had to be executed from Windows NT Performance Monitor to report performance alerts. This is not necessary in SQL Server 7.
In these exercises, you will execute a script that creates a multistep job to back up the StudyNwind transaction log. Next, you will use SQL Server Enterprise Manager to create a performance condition alert based on the Percent Log Used counter that notifies you and executes the backup job when the StudyNwind database transaction log has reached 60 percent capacity. Finally, you will verify that the condition alert works as expected.
In this exercise, you will execute a script that creates a multistep job to back up the StudyNwind transaction log.
This script creates a multistep job called Backup StudyNwind Log Alert that will back up the StudyNwind transaction log. Job steps also include contingencies to truncate the transaction log and back up the database if the BACKUP LOG statement fails.
NOTE
As this script is executing, you will receive warning messages that nonexistent steps are referenced. This is normal behavior. When each job step is created, actions to take upon success or failure are specified. The first few job steps reference steps that have not yet been created.
In this exercise, you will create a performance condition alert that fires when the Percent Log Used counter rises above 60 percent for the StudyNwind database.
Option | Value |
---|---|
Object | SQLServer:Databases |
Counter | Percent Log Used |
Instance | StudyNwind |
Alert if counter | rises above |
Value | 60 |
Option | Value |
---|---|
Execute job | Backup StudyNwind Log Alert |
Operators to notify | Admin by E-mail and Net send |
Delay between responses | 0 minutes, 0 seconds |
In this exercise, you will review and execute a script that generates activity in the StudyNwind database to fill the transaction log. Then you will verify that SQL Server Agent raised the alert and executed the job as defined.
This script simply generates activity in the StudyNwind database that fills the transaction log.
IMPORTANT
Do not allow the script to run for too long, or the transaction log backup file will grow very large.
The job history should display the most recent date and time that the job was executed successfully.
You can assign a fail-safe operator to respond to an alert when pager notifications to defined operators fail. For example, if an operator is off-duty when an alert fires, the fail-safe operator will be contacted.
A fail-safe operator is notified when
When you assign a fail-safe operator, consider the following facts:
Alerts make it possible to created automated responses to errors and other conditions on SQL Server. Alerts can be created based on error numbers, error severity levels, user-defined error messages, and performance conditions. In a large environment, multiple SQL Servers can forward events for processing on a single server, centralizing the administration of the alert responses. It is important that somebody always be notified when certain alerts occur; you can configure a fail-safe operator that can be notified if the normal operators cannot be notified.