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

Confirming Success or F ailure<br />

with Return Values<br />

You’ll see return values used in a couple of different ways. The first is to actually return data, such as<br />

an identity value or the number of rows that the sproc affected. Consider this an evil practice from the<br />

dark ages. Instead, move on to the way that return values should be used and what they are really there<br />

for — determining the execution status of your sproc.<br />

If it sounds like I have an opinion on how return values should be used, it’s because I most definitely do.<br />

I was actually originally taught to use return values as a “trick” to get around having to use output<br />

parameters — in effect, as a shortcut. Happily, I overcame this training. The problem is that, like<br />

most shortcuts, you’re cutting something out and, in this case, what you’re cutting out is rather<br />

important.<br />

Using return values as a means of returning data back to your calling routine clouds the meaning of the<br />

return code when you need to send back honest-to-goodness error codes. In short — don’t go there!<br />

Return values are all about indicating success or failure of the sproc, and even the extent or nature of<br />

that success or failure. For the C-style programmers among you, this should be a fairly easy strategy to<br />

relate to — it is a common practice to use a function’s return value as a success code, with any non-zero<br />

value indicating some sort of problem. If you stick with the default return codes in <strong>SQL</strong> <strong>Server</strong>, you’ll<br />

find that the same rules hold true.<br />

How to Use RETURN<br />

376<br />

Actually, your program will receive a return value whether you supply one or not. By default, <strong>SQL</strong><br />

<strong>Server</strong> automatically returns a value of zero when your procedure is complete.<br />

To pass a return value back from our sproc to the calling code, we simply use the RETURN statement:<br />

RETURN []<br />

Note that the return value must be an integer.<br />

Perhaps the biggest thing to understand about the RETURN statement is that it unconditionally exits from<br />

your sproc. That is, no matter where you are in your sproc, not one single more line of code will execute<br />

after you have issued a RETURN statement.<br />

By unconditionally, I don’t mean that a RETURN statement is executed regardless of where it is in code.<br />

On the contrary, you can have many RETURN statements in your sproc, and they will only be executed<br />

when the normal conditional structure of your code issues the command. Once that happens, however,<br />

there is no turning back.<br />

Let’s illustrate this idea of how a RETURN statement affects things by writing a very simple test sproc:<br />

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

GO

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

Saved successfully!

Ooh no, something went wrong!