This section gives you some suggested answers to the questions in Lab 6.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. 6.1.1 Answers
When the value of 15-JAN-2002 is entered for v_date , the number of the day of the week is determined for the variable v_day with the help of the TO_CHAR function. Next , each expression of the CASE statement is compared sequentially to the value of the selector. Because the value of the selector equals 3, the DBMS_OUTPUT.PUT_LINE statement associated with the third WHEN clause is executed. As a result, the message 'Today is Tuesday' is displayed on the screen. The rest of the expressions are not evaluated, and control is passed to the first executable statement after END CASE.
Notice that the last WHEN clause has been replaced by the ELSE clause. If '19-JAN-2002' is provided at runtime, the example produces the following output: Enter value for sv_user_date: 19-JAN-2002 old 2: v_date DATE := TO_DATE('&sv_user_date', 'DD-MON-YYYY'); new 2: v_date DATE := TO_DATE('19-JAN-2002', 'DD-MON-YYYY'); Today is Saturday PL/SQL procedure successfully completed. None of the expressions listed in the WHEN clauses are equal to the value of the selector because the date '19-JAN-2002' falls on Saturday, which is the seventh day of the week. As a result, the ELSE clause is executed, and the message 'Today is Saturday' is displayed on the screen.
Notice that in the new version of the example there is no need to declare variable v_day because the searched CASE statement does not need a selector. The expression that you used to assign a value to the variable v_day is now used as part of the searched conditions. When run, this example produces output identical to the output produced by the original version: Enter value for sv_user_date: 15-JAN-2002 old 2: v_date DATE := TO_DATE('&sv_user_date', 'DD-MON-YYYY'); new 2: v_date DATE := TO_DATE('15-JAN-2002', 'DD-MON-YYYY'); Today is Tuesday PL/SQL procedure successfully completed. 6.1.2 Answers
The searched conditions of the CASE statement are evaluated in sequential order. The searched condition WHEN v_final_grade >= 60 THEN yields TRUE, and as a result, letter "D" is assigned to the variable v_letter_grade . Control is then passed to the first executable statement after END IF, and the message "Letter grade is: D" is displayed on the screen.
If the value of the v_final_grade falls between 60 and 70, then the searched condition WHEN v_final_grade >= 70 THEN yields FALSE because the value of the variable v_final_grade is less that 70. However, the next searched condition WHEN v_final_grade >= 60 THEN of the CASE statement evaluates to TRUE, and letter "D" is assigned to the variable v_letter_grade .
All searched conditions of the CASE statement evaluate to FALSE because NULL cannot be compared to a value. Such a comparison will always yield FALSE, and as a result, the ELSE clause is executed.
In order to achieve the desired results, you are nesting CASE statements one inside the other just like IF statements in the previous chapter. The outer CASE statement evaluates the value of the variable v_final_grade . If the value of v_final_grade is NULL, then the message "There is no final grade." is displayed on the screen. If the value of v_final_grade is not NULL, then the ELSE part of the outer CASE statement is executed. Notice that in order to display the letter grade only when there is a final grade, you have associated the statement DBMS_OUTPUT.PUT_LINE ('Letter grade is: 'v_letter_grade); with the ELSE clause of the outer CASE statement. This guarantees that the message "Letter grade " will be displayed on the screen only when the variable v_final_grade is not NULL. In order to test this script fully, you have also introduced a substitution variable. This enables you to run the script for the different values of v_student_id . For the first run, enter value of 136, and for the second run enter the value of 102. The first output displays the message "There is no final grade." and does not display the message "Letter grade ": Enter value for sv_student_id: 136 old 2: v_student_id NUMBER := &sv_student_id; new 2: v_student_id NUMBER := 136; There is no final grade. PL/SQL procedure successfully completed. The second run produced output similar to the output produced by the original version: Enter value for sv_student_id: 102 old 2: v_student_id NUMBER := &sv_student_id; new 2: v_student_id NUMBER := 102; Letter grade is: A PL/SQL procedure successfully completed.
You learned earlier that the searched conditions are evaluated sequentially. Therefore, the statements associated with the first condition that yields TRUE are executed, and the rest of the searched conditions are discarded. In this example, the searched condition WHEN v_final_grade >= 60 THEN evaluates to TRUE, and the value of "D" is assigned to the variable v_letter_grade . Then control is passed to the first executable statement after END CASE, and the message "Letter grade is: D" is displayed on the screen. In order for this script to assign the letter grade correctly, the CASE statement may be modified as follows: CASE WHEN v_final_grade < 60 THEN v_letter_grade := 'F'; WHEN v_final_grade < 70 THEN v_letter_grade := 'D'; WHEN v_final_grade < 80 THEN v_letter_grade := 'C'; WHEN v_final_grade < 90 THEN v_letter_grade := 'B'; WHEN v_final_grade < 100 THEN v_letter_grade := 'A'; END CASE; However, there is a small problem with this CASE statement also. What do you think will happen when v_final_grade is greater than 100?
|