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

I’d love to say there’s some great workaround to this, but, unless you can somehow segment your recursive<br />

calls (run it 32 levels deep, then come all the way back out of the call stack, then run down it again),<br />

you’re pretty much out of luck. Just keep in mind that most recursive functions can be rewritten to be a<br />

more standard looping construct — which doesn’t have any hard limit. Be sure you can’t use a loop<br />

before you force yourself into recursion.<br />

Debugging<br />

Long ago and far away (<strong>SQL</strong> <strong>Server</strong> 2000), the Management Studio had real live debugging tools. They<br />

were a little clunky, in the sense that they really only worked around stored procedures (there wasn’t a way<br />

to debug just a script, and debugging triggers required you to create a sproc that would fire the trigger),<br />

but, with some work-arounds here and there, we had the long-sought-after debugger. <strong>SQL</strong> <strong>Server</strong> 2005<br />

came along and removed all debugging functionalityfrom the Management Studio (it was in the product,<br />

but you had to use the Visual Studio installation that is part of the Business Intelligence Development<br />

Studio in order to get at it — not very handy in any case, but nonexistent if you didn’t install BIDS for<br />

some reason). I’m happy to say that debugging is back in the Management Studio, and it’s better<br />

than ever!<br />

Starting the Debugger<br />

Unlike previous versions, the debugger in <strong>SQL</strong> <strong>Server</strong> <strong>2008</strong> is pretty easy to find. Much of using the<br />

debugger works as it does in VB or C# — probably like most modern debuggers, for that matter. Simply<br />

choose the Debug menu (available when a query window is active). You can then choose from options to<br />

get things started: Start Debugging (Alt+F5) or Step Into (F11).<br />

Let’s do a little bit of setup to show the debugger in action, both in a standard script and in a stored procedure<br />

scenario. To do this, we’ll use the script we were just working with in the previous section (to exercise<br />

the spTriangular stored procedure we also created earlier in the chapter). The script looked like this:<br />

DECLARE @WorkingOut int;<br />

DECLARE @WorkingIn int = 5;<br />

EXEC spTriangular @WorkingIn, @WorkingOut OUTPUT;<br />

PRINT CAST(@WorkingIn AS varchar) + ‘ Triangular is ‘<br />

+ CAST(@WorkingOut AS varchar);`<br />

With this script as the active query window, let’s start a debugging run with the Step Into option (choose<br />

it from the Debug menu or simply press F11).<br />

Parts of the Debugger<br />

402<br />

Several things are worth noticing when the Debugger window first comes up:<br />

❑ The yellow arrow on the left (Shown in Figure 12-2) indicates the current execution line — this is<br />

the next line of code that will be executed if we do a “go” or we start stepping through the code.

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

Saved successfully!

Ooh no, something went wrong!