Raising Your Own Error Messages
When something goes wrongperhaps the procedure expected the user to supply an invoice number, and the user failed to do soyou can raise custom error messages that get returned to the user.
I mentioned earlier that the SQL Server keeps track of all system errors in a system table called sysmessages. This table resides in the master database. The SQL Server has provided a system stored procedure to allow you to add your own messages to the sysmessages table.
This stored procedure is sp_addmessage. Here is an example of how you might add a message to sysmessages:
exec sp_addmessage 60000, 11, Customer ID not found in table
aq_customer
Once this message has been added to the other messages, you can raise the error by providing its number to the RAISERROR function:
raiserror (60000, 11, 1)
Msg 60000, Severity 11, State 1
Customer ID not found in table aq_customer
This example uses a different method for raising an error, providing the number instead of static text for the error. This method allows a defined error number to be passed back to the user.
Guidelines for Defining Your Own Errors
When you define your own error messages, there are a few things to keep in mind:
- Messages added to sysmessages must have a number greater than 50,000. Errors below that number have been reserved by Microsoft. (Its encouraging to know they have room in future releases for another 48,000 ways for the system to fail.)
- Since all user messages go to the same table, you must coordinate with the users of other databases to ensure that you do not attempt to add different messages with the same numbers. The system will prevent you from adding a new message with the same number.
- Only severity levels of 0 through 18 are available to you, unless you are the SA. Severities 0 through 10 are used to communicate a noncritical error; severities 11 through 16 are used to indicate varying degrees of user error; and severities 17 and 18 are system-level failures. These severities are seldom used because system-level errors often cause your SQL to abort, so the RAISERROR never executes. You learn more about RAISERROR when you explore stored procedures on Days 16 through 18.
Other Options for raiserror
Here is the full syntax statement for RAISERROR:
raiserror(msg_string, severity, state [WITH LOG])
raiserror (msg_id, severity, state [, arg1[, arg2 [, arg3...]]]
[WITH LOG])
The WITH LOG option is available only to system administrators. Use of this option will provide the error to the users screen and also copy it to the system log.
For the second type of RAISERROR, arguments are allowed. To use this, consider this example:
- I want to create an error message in a procedure that will show the customer name if that customer doesnt have an entry in the invoices table. Before implementing my application, I install an error:
exec sp_addmessage 60102, 14, Customer %s does not
exist in invoices.
- Notice that there is a %s in the middle of the string. When the error is raised, an argument is passed and the %s will be replaced with that argument:
raiserror(60102, 14, 1, Katrina VanAllen)
Msg 60102, Severity 14, State 1
Customer Katrina VanAllen does not exist in invoices.
The capability to pass arguments to error messages greatly increases the flexibility of the error system.
print Statement
Like RAISERROR, the PRINT statement returns a message to the user. PRINT statements are commonly used in stored procedures or in SQL code to communicate a non-critical message to the user. In applications, PRINT statements should be displayed in an unobtrusive manner for the user to observe. This example shows a simple PRINT statement:
Print The sky is falling!
The sky is falling!
Note: If you program DB-Library applications, a PRINT statement will come through to your message handler along with errors generated by RAISERROR. A print statement will always generate an error number and severity of zero.
Some Common Errors and What They Mean
In this section, I describe a few of the errors I see newcomers to SQL running into. Sometimes the error messages are not terribly communicative.
- Msg 170: Incorrect syntax near *: This error is usually due to a misspelled word, or running two words together. It is most often a problem with a SQL keyword (SELECT, FROM, and so on). The server will try to help by providing the general location of the failure with the near %s portion.
- Msg 207: Invalid column name UserID: The good news is that you have the table name correct. If you didnt, you would get the next error, #208. One possible cause is a misspelled column name. Column and table names (as well as all other objects on the server) are case sensitive, so assuming a table with a column named userid, this query would generate a 207 error:
select UserID from customer
- Msg 208: Invalid object name Authors: Very often, this indicates a misspelled or mixed-case table name. Sometimes, this error is because you are in a different database than you might have thought. The examples in this book use objects in the pubs database. Your current database is listed near the top of the window in ISQL/w, next to the DB label. A thorough discussion of databases and how they relate to SQL is in Day 11.
- Msg 230: SELECT permission denied on column audflags of object sysobjects in database master: In a multiuser system, some objects need to be protected from groups of users. This particular example shows an error that might occur if you attempt a SELECT * FROM SYSOBJECTS and your administrator has revoked permission to the audit flags column of the objects table. When you get this error, and you have just attempted a SELECT *, try to select some other columns in the table to determine whether you are allowed to access only certain columns, or whether the entire table is off-limits to you.
|
Используются технологии
uCoz