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.

12<br />

Stored Procedures<br />

Ah, the good stuff. If you’re a programmer coming from a procedural language, then this is probably<br />

the part you’ve been waiting for. It’s time to get down to the main variety of code of <strong>SQL</strong> <strong>Server</strong>,<br />

but before we get going too far down that road, I need to prepare you for what lies ahead. There’s<br />

probably a lot less than you’re expecting and, at the very same time, a whole lot more. The good<br />

news is that, with <strong>SQL</strong> <strong>Server</strong> <strong>2008</strong>, you have .NET support — giving us a veritable “oo la la!” of<br />

possibilities.<br />

You see, a stored procedure, sometimes referred to as a sproc (which I usually say as one word, but<br />

I’ve sometimes heard pronounced as “ess-proc”), is really just something of a script — or more<br />

correctly speaking, a batch — that is stored in the database rather than in a separate file. Now this<br />

comparison is not an exact one by any means — sprocs have things, such as input parameters, output<br />

parameters, and return values, that a script doesn’t really have, but the comparison is not that<br />

far off either.<br />

For now, <strong>SQL</strong> <strong>Server</strong>’s only “programming” language continues to be T-<strong>SQL</strong>, and that leaves us<br />

miles short of the kind of procedural horsepower that you expect when you think of a true programming<br />

language. However, T-<strong>SQL</strong> blows C, C++, Visual Basic, Java, Delphi, or whatever away<br />

when it comes to what T-<strong>SQL</strong> is supposed to do — work on data definition, manipulation, and<br />

access. But T-<strong>SQL</strong>’s horsepower stops right about there — at data access and management. In short,<br />

it has an adequate amount of power to get most simple things done, but it’s not always the place<br />

to do it.<br />

For this chapter, we’re not going to worry all that much about T-<strong>SQL</strong>’s shortcomings — instead,<br />

we’ll focus on how to get the most out of T-<strong>SQL</strong>, and even toss in a smattering of what .NET can<br />

add to the picture. We’ll take a look at parameters, return values, control of flow, looping structures,<br />

both basic and advanced error trapping, and more. In short, this is a big chapter that deals<br />

with many subjects. All of the major subject areas are broken up into their own sections, so you<br />

can take them one step at a time, but let’s start right out with the basics of getting a sproc created.

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

Saved successfully!

Ooh no, something went wrong!