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.

Chapter 12: Stored Procedures<br />

Note that this wasn’t a <strong>SQL</strong> <strong>Server</strong> error — as far as <strong>SQL</strong> <strong>Server</strong>’s concerned, everything about life is just<br />

fine. What’s nice, though, is that, were we using a client program (say one you wrote in C#, VB.NET, C++,<br />

or some other language), we would, as with our duplicate insert pay history item, be able to track the<br />

–1000 against a known constant and send a very specific message to the end user.<br />

Manually Raising Errors<br />

Sometimes you have errors that <strong>SQL</strong> <strong>Server</strong> doesn’t really know about, but you wish it did. For example,<br />

perhaps in the previous example you don’t want to return –1000. Instead, you’d like to be able to create<br />

a runtime error at the client end that the client would then use to invoke an error handler and act accordingly.<br />

To do this, you use the RAISERROR command in T-<strong>SQL</strong>. The syntax is pretty straightforward:<br />

RAISERROR (, , <br />

[, <br />

[,]] )<br />

[WITH option[,...n]]<br />

Let’s take a look at what these mean.<br />

Message ID/Message String<br />

The message ID or message string you provide determines which message is sent to the client.<br />

Using a message ID creates a manually raised error with the ID that you specified and the message that<br />

is associated with that ID as found in the sys.messages system view in the master database.<br />

If you want to see what your <strong>SQL</strong> <strong>Server</strong> has as predefined messages, you can always perform a<br />

SELECT * FROM master.sys.messages. This includes any messages you’ve manually added to<br />

your system using the sp_addmessage stored procedure or through the <strong>SQL</strong> <strong>Server</strong> Management<br />

Studio.<br />

You can also just supply a message string in the form of ad hoc text without creating a more permanent<br />

message in the system:<br />

RAISERROR (‘Hi there, I’’m an error’, 1, 1);<br />

This raises a rather simple error message:<br />

Hi there, I'm an error<br />

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

Notice that the assigned message number, even though you didn’t supply one, is 50000. This is the<br />

default error value for any ad hoc error. It can be overridden using the WITH SETERROR option.<br />

Severity<br />

390<br />

We got a quick overview of this when looking at TRY/CATCH in the chapter on scripting. For those of you<br />

already familiar with Windows servers, severity should be an old friend. Severity is an indication of just<br />

how bad things really are based on this error. For <strong>SQL</strong> <strong>Server</strong>, however, what severity codes mean can<br />

get a little bizarre. They can range from informational (severities 1–18), to system level (19–25), and even

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

Saved successfully!

Ooh no, something went wrong!