Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
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
- Page 393 and 394: A Simple CASE A simple CASE takes a
- Page 395 and 396: 3 8 More Than One Apart 2 2 Ends Wi
- Page 397 and 398: Now, I don’t know about you, but
- Page 399 and 400: The WAITFOR statement does exactly
- Page 401 and 402: IF @ErrorNo = 2714 -- Object exists
- Page 403: Chapter 11: Writing Scripts and Bat
- Page 406 and 407: Chapter 12: Stored Procedures Creat
- Page 408 and 409: Chapter 12: Stored Procedures Dropp
- Page 410 and 411: Chapter 12: Stored Procedures Suppl
- Page 412 and 413: Chapter 12: Stored Procedures 374 [
- Page 414 and 415: Chapter 12: Stored Procedures Confi
- Page 416 and 417: Chapter 12: Stored Procedures Now,
- Page 418 and 419: Chapter 12: Stored Procedures SQL S
- Page 420 and 421: Chapter 12: Stored Procedures 382 c
- Page 422 and 423: Chapter 12: Stored Procedures It wo
- Page 424 and 425: Chapter 12: Stored Procedures 386 r
- Page 426 and 427: Chapter 12: Stored Procedures 388 -
- Page 428 and 429: Chapter 12: Stored Procedures Note
- Page 430 and 431: Chapter 12: Stored Procedures 392 n
- Page 432 and 433: Chapter 12: Stored Procedures All t
- Page 434 and 435: Chapter 12: Stored Procedures Sproc
- Page 436 and 437: Chapter 12: Stored Procedures When
- Page 438 and 439: Chapter 12: Stored Procedures 400 @
- Page 440 and 441: Chapter 12: Stored Procedures I’d
- Page 442 and 443: Chapter 12: Stored Procedures match
- Page 446 and 447: Chapter 12: Stored Procedures 408 f
- Page 449 and 450: 13 User-Defined Functions Well, her
- Page 451 and 452: types!), except for BLOBs, cursors,
- Page 453 and 454: We get back the same set as with th
- Page 455 and 456: AS RETURN (SELECT BusinessEntityID,
- Page 457 and 458: in your relational database. These
- Page 459 and 460: AS BEGIN ( EmployeeID int NOT NULL,
- Page 461 and 462: So, as you can see, we can actually
- Page 463 and 464: Despite being schema-bound, this on
- Page 465 and 466: 14 Transactions and Locks This is o
- Page 467 and 468: we are unable or do not want to com
- Page 469 and 470: Figure 14-1 Data needed Data in cac
- Page 471 and 472: Transaction 4 This transaction wasn
- Page 473 and 474: Oops — problem!!! Transaction 2 h
- Page 475 and 476: The only cure for this is setting y
- Page 477 and 478: Exclusive Locks Exclusive locks are
- Page 479 and 480: Also: ❑ The Sch-S is compatible w
- Page 481 and 482: The syntax for switching between th
- Page 483 and 484: As with most things in life, howeve
- Page 485 and 486: purchased. Process 2 records sales;
- Page 487: Chapter 14: Transactions and Locks
- Page 490 and 491: Chapter 15: Triggers the world’s
- Page 492 and 493: Chapter 15: Triggers WITH ENCRYPTIO
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