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.

Let’s use this in a simple example:<br />

RAISERROR (‘This is a sample parameterized %s, along with a zero<br />

padding and a sign%+010d’,1,1, ‘string’, 12121);<br />

If you execute this, you get back something that looks a little different from what’s in the quotation<br />

marks:<br />

This is a sample parameterized string, along with a zero<br />

padding and a sign+000012121<br />

Msg 50000, Level 1, State 1<br />

The extra values supplied were inserted, in order, into your placeholders, with the final value being<br />

reformatted as specified.<br />

WITH <br />

Currently, you can mix and match three options when you raise an error:<br />

❑ LOG<br />

❑ SETERROR<br />

❑ NOWAIT<br />

WITH LOG<br />

This tells <strong>SQL</strong> <strong>Server</strong> to log the error to the <strong>SQL</strong> <strong>Server</strong> error log and the Windows Application Log. This<br />

option is required with severity levels that are 19 or higher.<br />

WITH SETERROR<br />

By default, a RAISERROR command doesn’t set @@ERROR with the value of the error you generated.<br />

Instead, @@ERROR reflects the success or failure of your actual RAISERROR command. SETERROR overrides<br />

this and sets the value of @@ERROR to be equal to your error ID.<br />

WITH NOWAIT<br />

Immediately notifies the client of the error.<br />

Adding Your Own Custom Error Messages<br />

We can make use of a special system stored procedure to add messages to the system. The sproc is called<br />

sp_addmessage, and the syntax looks like this:<br />

sp_addmessage [@msgnum =] ,<br />

[@severity =] ,<br />

[@msgtext =] <br />

[, [@lang =] ]<br />

[, [@with_log =] [TRUE|FALSE]]<br />

[, [@replace =] ‘replace‘]<br />

Chapter 12: Stored Procedures<br />

393

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

Saved successfully!

Ooh no, something went wrong!