17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

catastrophic (20–25). If you raise an error of severity 19 or higher (system level), the WITH LOG option<br />

must also be specified; 20 and higher automatically terminates the users’ connections. (Kind of fun if<br />

you’re feeling particularly ornery, but they hate that!)<br />

So, let’s get back to what I meant by bizarre. <strong>SQL</strong> <strong>Server</strong> actually varies its behavior into more ranges than<br />

Windows does, or even than the Books Online will tell you about. Errors fall into six major groupings:<br />

State<br />

State is an ad hoc value. It’s something that recognizes that exactly the same error may occur at multiple<br />

places within your code. The notion is that this gives you an opportunity to send something of a place<br />

marker for where exactly the error occurred.<br />

State values can be between 1 and 127. If you are troubleshooting an error with <strong>Microsoft</strong> tech support,<br />

they apparently have some arcane knowledge that hasn’t been shared with you about what some of<br />

these mean. I’m told that if you make a tech-support call to <strong>Microsoft</strong>, they are likely to ask about and<br />

make use of this state information.<br />

Error Arguments<br />

Chapter 12: Stored Procedures<br />

1–10 Purely informational, but will return the specific error code in the message information<br />

11–16 If you do not have a TRY/CATCH block set up, then these terminate execution of the procedure<br />

and raise an error at the client. The state is shown to be whatever value you set it<br />

to. If you have a TRY/CATCH block defined, then that handler will be called rather than<br />

raising an error at the client.<br />

17 Usually, only <strong>SQL</strong> <strong>Server</strong> should use this severity. Basically, it indicates that <strong>SQL</strong> <strong>Server</strong><br />

has run out of resources — for example, tempdb was full — and can’t complete the<br />

request. Again, a TRY/CATCH block will get this before the client does.<br />

18–19 Both of these are severe errors and imply that the underlying cause requires system<br />

administrator attention. With 19, the WITH LOG option is required, and the event will<br />

show up in the Windows Event Log. These are the final levels at which you can trap the<br />

error with a TRY/CATCH block — after this, it will go straight to the client.<br />

20–25 Your world has just caved in, as has the user’s connection. Essentially, this is a fatal<br />

error. The connection is terminated. As with 19, you must use the WITH LOG option and<br />

a message will, if applicable, show up in the Event Log.<br />

Some predefined errors accept arguments. These allow you to make the error somewhat more dynamic<br />

by changing to the specific nature of the error. You can also format your error messages to accept<br />

arguments.<br />

When you want to use dynamic information in what is otherwise a static error message, you need to format<br />

the fixed portion of your message so that it leaves room for the parameterized section of the message.<br />

You do so by using placeholders. If you’re coming from the C or C++ world, then you’ll recognize<br />

the parameter placeholders immediately; they are similar to the printf command arguments. If you’re<br />

391

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!