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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12: Stored Procedures<br />

All the parameters mean pretty much the same thing that they did with RAISERROR, except for the addition<br />

of the language and replace parameters and a slight difference with the WITH LOG option.<br />

@lang<br />

This specifies the language to which this message applies. What’s cool here is that you can specify a separate<br />

version of your message for any language supported in syslanguages.<br />

@with_log<br />

This works just the same as it does in RAISERROR in that, if set to TRUE, the message will be automatically<br />

logged to both the <strong>SQL</strong> <strong>Server</strong> error log and the NT application log when raised (the latter only when<br />

running under NT). The only trick here is that you indicate that you want this message to be logged by<br />

setting this parameter to TRUE rather than using the WITH LOG option.<br />

@replace<br />

Be careful of this one in the Books Online. Depending on how you read it, it would<br />

be easy to interpret it as saying that you should set @with_log to a string constant of<br />

‘WITH_LOG’, when you should set it to TRUE. Perhaps even more confusing is that<br />

the REPLACE option looks much the same, and it must be set to the string constant<br />

rather than TRUE.<br />

If you are editing an existing message rather than creating a new one, then you must set the @replace<br />

parameter to ‘REPLACE’. If you leave this off, you’ll get an error if the message already exists.<br />

Creating a set list of additional messages for use by your applications can greatly<br />

enhance reuse, but more importantly, it can significantly improve readability of<br />

your application. Imagine if every one of your database applications made use of a<br />

constant list of custom error codes. You could then easily establish a constants file (a<br />

resource or include library, for example) that had a listing of the appropriate errors;<br />

you could even create an include library that had a generic handling of some or all<br />

of the errors. In short, if you’re going to be building multiple <strong>SQL</strong> <strong>Server</strong> apps in the<br />

same environment, consider using a set list of errors that is common to all your applications.<br />

Keep in mind, however, that many system administrators do not like applicationspecific<br />

changes to affect the master database (which is where your custom error<br />

message is written), so if you are not in control of your own server, make sure that<br />

custom error messages will be allowed on the server before writing code that depends<br />

on them.<br />

Using sp_addmessage<br />

394<br />

As has already been indicated, sp_addmessage creates messages in much the same way as we create ad<br />

hoc messages using RAISERROR.

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

Saved successfully!

Ooh no, something went wrong!