
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. 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
