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.

If we create our sproc with this option, then the sproc will be recompiled each time that it runs, regardless<br />

of other options chosen at runtime.<br />

Extended Stored Procedures (XPs)<br />

The advent of .NET in <strong>SQL</strong> <strong>Server</strong> has really changed the area of Extended Stored Procedures. These<br />

used to be the bread and butter of the hard-core code scenarios — when you hit those times where basic<br />

T-<strong>SQL</strong> and the other features of <strong>SQL</strong> <strong>Server</strong> just wouldn’t give you what you needed.<br />

With the availability of .NET to deal with things like O/S file access and other external communication<br />

or complex formulas, the day of the XP would seem to be waning. XPs still have their place in the world<br />

for few reasons:<br />

❑ Times where performance is so critical that you want the code running genuinely in process to<br />

<strong>SQL</strong> <strong>Server</strong> (this is truly a radical approach in the .NET era)<br />

❑ Situations where your administrators will not allow .NET code to execute for security reasons<br />

(though allowing XPs, but not .NET, is somewhat silly if you ask me …)<br />

For purposes of this book, I’ll merely say that <strong>SQL</strong> <strong>Server</strong> does allow for the idea of externally written<br />

code that runs as a .DLL in process with <strong>SQL</strong> <strong>Server</strong>. XPs are created using C or C++.<br />

A Brief Look at Recur sion<br />

Recursion is one of those things that isn’t used very often in programming. Still, it’s also one of those<br />

things for which, when you need it, there never seems to be anything else that will quite do the trick. As<br />

a “just in case,” a brief review of what recursion is seems in order.<br />

The brief version is that recursion is the situation where a piece of code calls itself. The dangers here<br />

should be fairly self-evident — if it calls itself once, then what’s to keep it from calling itself over and<br />

over again? The answer to that is you. That is, you need to make sure that if your code is going to be<br />

called recursively, you provide a recursion check to make sure you bail out when it’s appropriate.<br />

I’d love to say that the example I’m going to use is all neat and original — but it isn’t. Indeed, for an example,<br />

I’m going to use the classic recursion example that’s used with about every textbook recursion discussion<br />

I’ve ever seen — please accept my apologies now. It’s just that it’s an example that can be understood by<br />

just about anyone, so here we go.<br />

So what is that classic example? Factorials. For those who have had a while since math class (or their last<br />

recursion discussion), a factorial is the value you get when you take a number and multiply it successively<br />

by that number less one, then the next value less one, and so on, until you get to one. For example, the<br />

factorial of 5 is 120 — that’s 5*4*3*2*1.<br />

So, let’s look at an implementation of such a recursive sproc:<br />

CREATE PROC spFactorial<br />

@ValueIn int,<br />

Chapter 12: Stored Procedures<br />

399

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

Saved successfully!

Ooh no, something went wrong!