In this lesson, you learn how to use the debugger to identify errors in stored procedures. The SQL Anywhere sample database, demo.db, contains a stored procedure named debugger_tutorial, which contains a deliberate error. The debugger_tutorial procedure should return a result set that contains the name of the company that has placed the highest value of orders, and the value of their orders. It computes these values by looping over the result set of a query that lists companies and orders. (This result could be achieved without adding the logic into the procedure by using a SELECT FIRST query. The procedure is used to create a convenient example.) However, the bug contained in the debugger_tutorial procedure results in its failure to return the specified result set. In this lesson, you run the stored procedure and diagnose and fix the bug.
In the left pane of Sybase Central, double-click Procedures & Functions.
Right-click Debugger_Tutorial (GROUPO) and click Execute From Interactive SQL.
Interactive SQL opens and the following result set appears:
top_company | top_value |
---|---|
(NULL) | (NULL) |
This is an incorrect result. The remainder of the tutorial diagnoses the error that produced this result.
Close Interactive SQL.
To diagnose the bug in the procedure, set breakpoints in the procedure, beginning with the first executable statement. Step through the code, watching the value of variables as the procedure is executed.
Click Mode » Debug.
In the right pane, double-click Debugger_Tutorial (GROUPO).
In the right pane, locate the following statement:
OPEN cursor_this_customer; |
To add a breakpoint, click the vertical gray area to the left of the statement. The breakpoint appears as a red circle.
In the left pane, right-click Debugger_Tutorial (GROUPO) and click Execute From Interactive SQL.
In the right pane of Sybase Central, a yellow arrow appears on top of the red circle indicating the break point.
In the Debugger Details window, click the Local tab to display a list of local variables in the procedure together with their current value and data type. The Top_Company, Top_Value, This_Value, and This_Company variables are all uninitialized and are therefore NULL.
Press F11 to scroll through the procedure. The value of the variables changes when you reach the following line:
IF SQLSTATE = error_not_found THEN |
Press F11 once more to determine which branch the execution takes. The yellow arrow moves back to the following text:
customer_loop: loop |
The IF
test did not return true. The test failed because a comparison of any value to NULL returns NULL. A value of NULL fails the
test and the code inside the IF
...END IF
statement is not executed.
At this point, you may realize that the problem is that Top_Value is not initialized.
You can test the hypothesis that the problem is the lack of initialization for Top_Value without changing the procedure code.
In the Debugger Details window, click the Local tab.
Click the Top_Value variable and type 3000 in the Value field and press Enter.
Press F11 repeatedly until the Value field of the This_Value variable is greater than 3000.
Click the breakpoint so that it turns gray.
Press F5 to execute the procedure.
The Interactive SQL window appears again. It shows the correct results.
top_company | top_value |
---|---|
Chadwicks | 8076 |
Close Interactive SQL.
The hypothesis is confirmed. The problem is that the Top_Value is not initialized.
Click Mode » Design.
In the right pane, locate the following statement:
OPEN cursor_this_customer; |
Type a new line underneath that initializes the Top_Value variable:
SET top_value = 0; |
Click File » Save.
Execute the procedure again, and confirm that Interactive SQL displays the correct results.
You have now completed the lesson. Close any open Interactive SQL windows.
Discuss this page in DocCommentXchange.
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |