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 />

Now, just for grins, let’s alter that sproc to verify that we can send whatever integer value we want back<br />

as the return value:<br />

USE AdventureWorks<strong>2008</strong>;<br />

GO<br />

ALTER PROC spTestReturns<br />

AS<br />

DECLARE @MyMessage varchar(50);<br />

DECLARE @MyOtherMessage varchar(50);<br />

SELECT @MyMessage = ‘Hi, it’’s that line before the RETURN’;<br />

PRINT @MyMessage<br />

RETURN 100;<br />

SELECT @MyOtherMessage = ‘Sorry, but we won’’t get this far’;<br />

PRINT @MyOtherMessage;<br />

RETURN;<br />

Now rerun your test script and you’ll get the same result save for that change in return value:<br />

Hi, it’s that line before the RETURN<br />

-----------<br />

100<br />

(1 row(s) affected)<br />

More On Dealing with Er rors<br />

378<br />

Sure. We don’t need this section. I mean, our code never has errors, and we never run into problems,<br />

right? OK, well, now that we’ve had our moment of fantasy for today, let’s get down to reality. Things<br />

go wrong — it’s just the way that life works in the wonderful world of software engineering. Fortunately,<br />

we can do something about it. Unfortunately, you’re probably not going to be happy with the<br />

tools you have. Fortunately again, there are ways to make the most out of what you have, and ways to<br />

hide many of the inadequacies of error handling in the <strong>SQL</strong> world.<br />

OK, so we got a taste of error handling at the end of the previous chapter when we looked at TRY/CATCH<br />

blocks. Indeed, those are the way to perform traditional error handling when you only need to support<br />

<strong>SQL</strong> <strong>Server</strong> 2005 and newer. There is, however, a lot more to think about in <strong>SQL</strong> <strong>Server</strong> errors and error<br />

handling than just TRY/CATCH. So let’s take a bit deeper look at things. . . .<br />

Three common error types can happen in <strong>SQL</strong> <strong>Server</strong>:<br />

❑ Errors that create runtime errors and stop your code from proceeding further.<br />

❑ Errors that <strong>SQL</strong> <strong>Server</strong> knows about, but that don’t create runtime errors such that your code<br />

stops running (these can also be referred to as inline errors).<br />

❑ Errors that are more logical in nature and to which <strong>SQL</strong> <strong>Server</strong> is essentially oblivious.

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

Saved successfully!

Ooh no, something went wrong!