Lab 10.1 Exercise AnswersThis section gives you some suggested answers to the questions in Lab 10.1, with discussion related to how those answers resulted. The most important thing to realize is whether your answer works. You should figure out the implications of the answers here and what the effects are from any different answers you may come up with. 10.1.1 Answers
The SELECT INTO statement returns a value of 10. However, the variable v_total has been defined so that it is able to hold only single digit numbers. Because 10 is a two-digit number, the error occurs during the execution of the SELECT INTO statement. As a result, an error message is displayed on the screen. The following output contains only a portion of the output produced by the example: There is(are) 1 student(s) for zipcode 01247 … There is(are) 1 student(s) for zipcode 07023 DECLARE * ERROR at line 1: ORA-06502: PL/SQL: numeric or value error: number precision too large ORA-06512: at line 13 Notice that as soon as the error occurs, the example terminates because there is no exception handler for this error.
When run, this version of the example produces the following output (only a portion of the output is shown): There is(are) 1 student(s) for zipcode 01247 … There is(are) 1 student(s) for zipcode 07023 An error has occurred PL/SQL procedure successfully completed. Notice that because an exception handler has been added to the script, it was able to terminate successfully.
In order for the cursor loop to be able to execute after an exception has occurred, the exception handler must be moved inside the loop in the inner block. In this case, once an exception has occurred, control is transferred to the exception handler of the block. Once the exception is raised, control is passed to the next executable statement of the outer block. That statement is END LOOP. If the end of the loop has not been reached and there are more records to process, control is passed to the top of the loop, and the inner block is executed again. As a result, this version of the script produces the following output (again, only a portion of the output is shown): There is(are) 1 student(s) for zipcode 01247 … There is(are) 1 student(s) for zipcode 07023 An error has occurred There is(are) 1 student(s) for zipcode 07029 There is(are) 2 student(s) for zipcode 07036 There is(are) 1 student(s) for zipcode 07040 There is(are) 5 student(s) for zipcode 07042 There is(are) 1 student(s) for zipcode 07044 There is(are) 5 student(s) for zipcode 07047 Done... PL/SQL procedure successfully completed. |