Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 12: Stored Procedures There are a whole host of commands available to Intellisense, but you’ll find that most of these are not actually available when debugging. Using the Debugger Once It’s Started 406 Now that we have the preliminaries out of the way and the debugger window up, we’re ready to start walking through our code. If you were walking through some of the descriptions before, stop the debugger and restart it so we’re in the same place. The first executable line of our sproc is a bit deceptive — it is the DELCARE statement for @WorkingIn. Normally variable declarations are not considered executable, but, in this case, we are initializing the variable as part of the declaration, so the initialization code is seen by the debugger. You should notice that none of our variables has yet been set (the initialization code will be next to run, but has not actually executed yet). Step forward (using the menu choice, the tool tip, or simply press F11) and you should see (via the Locals window) @WorkingIn get initialized to our value of 5 — @WorkingOut is not initialized as part of the declaration. Use the Step Into key one more time. We enter into our first execution of the spTriangular stored procedure and land at the first executable line in the sproc — our IF statement. Since the value of @ValueIn is indeed not equal to 1, we step into the BEGIN...END block specified by our IF statement. Specifically, we move to our SELECT statement that initializes the @InWorking parameter for this particular execution of the procedure. As we’ll see later, if the value of @ValueIn had indeed been one, we would have immediately dropped down to our ELSE statement. Again, step forward one line by pressing F11, or using the Step Into icon or menu choice, until just before you enter the next instance of spTriangular. Pay particular attention to the value of @InWorking in the Locals window. Notice that it changed to the correct value (@ValueIn is currently 5, so 5–1 is 4) as set by our SELECT statement. Also notice that our Call Stack window has only the current instance of our sproc in it (plus the current statement) — since we haven’t stepped down into our nested versions of the sproc yet, we only see one instance. Now go ahead and step into our next statement. Since this is the execution of a sproc, we’re going to see a number of different things change in the debugger. Notice that it appears that our arrow that indicates the current statement jumped back up to the IF statement. Why? Well, this is a new instance of what is otherwise the same sproc. We can tell this based on our Call Stack window — notice that it now has two instances of our sproc listed. The one at the top (with the yellow arrow) is the current instance, and the one with the red breakpoint dot is a parent instance that is now waiting for something further up in the call stack. Notice also that the @ValueIn parameter has the value of 4 — that is the value we passed in from the outer instance of the sproc. If you want to see the value of variables in the scope of the outer instance of the sproc, just double-click on that instance’s line in the Call Stack window (the one with the green arrow) and you’ll see several things changed in our debugging windows. There are two things to notice here. First, the values of our variables have changed back to those in the scope of the outer (and currently selected) instance of the sproc. Second, the icon for our current execution

line is different. This new green arrow is meant to show that this is the current line in this instance of the sproc, but it is not the current line in the overall call stack. Go back to the current instance by clicking on the top item in the Call Stack window. Then step in three more times. This should bring you to the top line (the IF statement) in our third instance of the sproc. Notice that our call stack has become three deep and that the values of our variables and parameters in the Locals window have changed again. Last, but not least, notice that this time our @ValueIn parameter has a value of 3. Repeat this process until the @ValueIn parameter has a value of 1. Step into the code one more time and you’ll see a slight change in behavior. This time, since the value in @ValueIn is equal to 1, we move into the BEGIN...END block defined with our ELSE statement. Since we’ve reached the bottom, we’re ready to start going back up the call stack. Use Step Into through the last line of our procedure and you’ll find that our call stack is back to only four levels. Also, notice that our output parameter (@OutWorking) has been appropriately set. This time, let’s do something different and do a Step Out (Shift+F11). If you’re not paying attention, it will look like absolutely nothing has changed. So, you should now be able to see how the Debugger can be very handy indeed. .NET Assemblies Chapter 12: Stored Procedures In this case, to use the old cliché, looks are deceiving. Again, notice the change in the Call Stack window and in the values in the Locals window — we stepped out of what was then the current instance of the sproc and moved up a level in the Call Stack. If we now keep stepping into the code (F11), then our sproc has finished running and we’ll see the final version of our status windows and their respective finishing values. A big word of caution here! If you want to be able to see the truly final values (such as an output parameter being set), make sure that you use the Step Into option to execute the last line of code. If you use an option that executes several lines at once, such as a Go or Step Out, all you will get is the output window without any final variable information. A work-around is to place a break point on the last point at which you expect to perform a RETURN in the outermost instance of your sproc. That way, you can run in whatever debug mode you want, but still have execution halt in the end so you can inspect your final variables. Because of just how wide open a topic assemblies are, as well as their potential to add exceptional complexity to your database, these are largely considered out of scope for this title, save for one thing — letting you know they are there. .NET assemblies can be associated with your system and utilized to provide the power behind truly complex operations. You could, just as an example, use a .NET assembly in a user-defined function to provide data from an external data source (perhaps one that has to be called on the fly, such as a new 407

Chapter 12: Stored Procedures<br />

There are a whole host of commands available to Intellisense, but you’ll find that most of these are not<br />

actually available when debugging.<br />

Using the Debugger Once It’s Started<br />

406<br />

Now that we have the preliminaries out of the way and the debugger window up, we’re ready to start<br />

walking through our code. If you were walking through some of the descriptions before, stop the debugger<br />

and restart it so we’re in the same place.<br />

The first executable line of our sproc is a bit deceptive — it is the DELCARE statement for @WorkingIn.<br />

Normally variable declarations are not considered executable, but, in this case, we are initializing the<br />

variable as part of the declaration, so the initialization code is seen by the debugger. You should notice<br />

that none of our variables has yet been set (the initialization code will be next to run, but has not actually<br />

executed yet). Step forward (using the menu choice, the tool tip, or simply press F11) and you should see<br />

(via the Locals window) @WorkingIn get initialized to our value of 5 — @WorkingOut is not initialized<br />

as part of the declaration.<br />

Use the Step Into key one more time. We enter into our first execution of the spTriangular stored procedure<br />

and land at the first executable line in the sproc — our IF statement.<br />

Since the value of @ValueIn is indeed not equal to 1, we step into the BEGIN...END block specified by<br />

our IF statement. Specifically, we move to our SELECT statement that initializes the @InWorking parameter<br />

for this particular execution of the procedure. As we’ll see later, if the value of @ValueIn had indeed<br />

been one, we would have immediately dropped down to our ELSE statement.<br />

Again, step forward one line by pressing F11, or using the Step Into icon or menu choice, until just before<br />

you enter the next instance of spTriangular.<br />

Pay particular attention to the value of @InWorking in the Locals window. Notice that it changed to the<br />

correct value (@ValueIn is currently 5, so 5–1 is 4) as set by our SELECT statement. Also notice that our<br />

Call Stack window has only the current instance of our sproc in it (plus the current statement) — since<br />

we haven’t stepped down into our nested versions of the sproc yet, we only see one instance.<br />

Now go ahead and step into our next statement. Since this is the execution of a sproc, we’re going to see<br />

a number of different things change in the debugger. Notice that it appears that our arrow that indicates<br />

the current statement jumped back up to the IF statement. Why? Well, this is a new instance of what is<br />

otherwise the same sproc. We can tell this based on our Call Stack window — notice that it now has two<br />

instances of our sproc listed. The one at the top (with the yellow arrow) is the current instance, and the<br />

one with the red breakpoint dot is a parent instance that is now waiting for something further up in the<br />

call stack. Notice also that the @ValueIn parameter has the value of 4 — that is the value we passed in<br />

from the outer instance of the sproc.<br />

If you want to see the value of variables in the scope of the outer instance of the sproc, just double-click<br />

on that instance’s line in the Call Stack window (the one with the green arrow) and you’ll see several<br />

things changed in our debugging windows.<br />

There are two things to notice here. First, the values of our variables have changed back to those in the<br />

scope of the outer (and currently selected) instance of the sproc. Second, the icon for our current execution

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

Saved successfully!

Ooh no, something went wrong!