btatube.blogg.se

Oracle 10g programming a primer
Oracle 10g programming a primer






oracle 10g programming a primer

WHEN grade = 'A' THEN DBMS_OUTPUT.PUT_LINE('Excellent') For details, see "CASE Expressions".Įxample 4-7 Using the Searched CASE Statement That is, normal execution stops and control transfers to the exception-handling part of your PL/SQL block or subprogram.Īn alternative to the CASE statement is the CASE expression, where each WHEN clause is an expression. Optionally, the label name can also appear at the end of the CASE statement.Įxceptions raised during the execution of a CASE statement are handled in the usual way. The label, an undeclared identifier enclosed by double angle brackets, must appear at the beginning of the CASE statement. Like PL/SQL blocks, CASE statements can be labeled. The CASE statement has the following form: These two keywords must be separated by a space. The keywords END CASE terminate the CASE statement. If the CASE statement does not match any of the WHEN clauses and you omit the ELSE clause, PL/SQL raises the predefined exception CASE_NOT_FOUND. There is always a default action, even when you omit the ELSE clause. However, if you omit the ELSE clause, PL/SQL adds the following implicit ELSE clause: In the last example, if the grade is not one of the choices covered by a WHEN clause, the ELSE clause is selected, and the phrase 'No such grade' is output. The ELSE clause works similarly to the ELSE clause in an IF statement. Execution never falls through if any WHEN clause is executed, control passes to the next statement. For instance, in the last example, if grade equals 'C', the program outputs 'Good'. If the value of the selector equals the value of a WHEN-clause expression, that WHEN clause is executed. The value of the selector determines which clause is executed. The selector is followed by one or more WHEN clauses, which are checked sequentially. The value it yields can have any PL/SQL datatype other than BLOB, BFILE, an object type, a PL/SQL record, an index-by-table, a varray, or a nested table.

oracle 10g programming a primer

The selector expression is evaluated only once. Usually, however, it consists of a single variable. For example, it can contain function calls. The selector expression can be arbitrarily complex. The keyword is followed by a selector, which is the variable grade in the last example. The CASE statement begins with the keyword CASE. When possible, rewrite lengthy IF-THEN-ELSIF statements as CASE statements. The CASE statement is more readable and more efficient. WHEN 'F' THEN DBMS_OUTPUT.PUT_LINE('Poor') ĮLSE DBMS_OUTPUT.PUT_LINE('No such grade') WHEN 'D' THEN DBMS_OUTPUT.PUT_LINE('Fair') WHEN 'C' THEN DBMS_OUTPUT.PUT_LINE('Good') WHEN 'B' THEN DBMS_OUTPUT.PUT_LINE('Very Good') WHEN 'A' THEN DBMS_OUTPUT.PUT_LINE('Excellent') Example 4-6 Using the CASE-WHEN Statement








Oracle 10g programming a primer