Sie sind auf Seite 1von 4

CASE Expressions A CASE expression selects a result from one or more alternatives, and returns the result.

The CASE expression uses a selector, an expression whose value determines which alternative to return. A CASE expression has the following form: CASE selector WHEN expression1 THEN result1 WHEN expression2 THEN result2 ... WHEN expressionN THEN resultN [ELSE resultN+1] END; The selector is followed by one or more WHEN clauses, which are checked sequentially. The value of the selector determines which clause is executed. The first WHEN clause that matches the value of the selector determines the result value, and subsequent WHEN clauses are not evaluated. An example follows: DECLARE grade CHAR(1) := 'B'; appraisal VARCHAR2(20); BEGIN appraisal := CASE grade WHEN 'A' THEN 'Excellent' WHEN 'B' THEN 'Very Good' WHEN 'C' THEN 'Good' WHEN 'D' THEN 'Fair' WHEN 'F' THEN 'Poor' ELSE 'No such grade' END; END; The optional ELSE clause works similarly to the ELSE clause in an IF statement. If the value of the selector is not one of the choices covered by a WHEN clause, the ELSE clause is executed. If no ELSE clause is provided and none of the WHEN clauses are matched, the expression returns NULL. An alternative to the CASE expression is the CASE statement, where each WHEN clause can be an entire PL/SQL block.. Searched CASE Expression PL/SQL also provides a searched CASE expression, which has the form: CASE WHEN search_condition1 THEN result1 WHEN search_condition2 THEN result2 ... WHEN search_conditionN THEN resultN [ELSE resultN+1] END; A searched CASE expression has no selector. Each WHEN clause contains a search condition that yields a Boolean value, which lets you test different variables or multiple conditions in a single WHEN clause. An example follows: DECLARE grade CHAR(1); appraisal VARCHAR2(20); BEGIN ... appraisal := CASE WHEN grade = 'A' THEN 'Excellent' WHEN grade = 'B' THEN 'Very Good' WHEN grade = 'C' THEN 'Good' WHEN grade = 'D' THEN 'Fair' WHEN grade = 'F' THEN 'Poor' ELSE 'No such grade' END; ... END; The search conditions are evaluated sequentially. The Boolean value of each search condition determines which WHEN clause is executed. If a search condition yields TRUE, its WHEN clause is executed. After any WHEN clause is executed, subsequent search conditions are not evaluated. If none of the search conditions yields TRUE, the optional ELSE clause is executed. If no WHEN clause is executed and no ELSE clause is supplied, the value of the expression is NULL. Handling Null Values in Comparisons and Conditional Statements

When working with nulls, you can avoid some common mistakes by keeping in mind the following rules: Comparisons involving nulls always yield NULL Applying the logical operator NOT to a null yields NULL In conditional control statements, if the condition yields NULL, its associated sequence of statements is not executed If the expression in a simple CASE statement or CASE expression yields NULL, it cannot be matched by using WHEN NULL. In this case, you would need to use the searched case syntax and test WHEN expression IS NULL. In the example below, you might expect the sequence of statements to execute because x and y seem unequal. But, nulls are indeterminate. Whether or not x is equal to y is unknown. Therefore, the IF condition yields NULL and the sequence of statements is bypassed. x := 5; y := NULL; ... IF x != y THEN -- yields NULL, not TRUE sequence_of_statements; -- not executed END IF; In the next example, you might expect the sequence of statements to execute because a and b seem equal. But, again, that is unknown, so the IF condition yields NULL and the sequence of statements is bypassed. a := NULL; b := NULL; ... IF a = b THEN -- yields NULL, not TRUE sequence_of_statements; -- not executed END IF; NOT Operator Recall that applying the logical operator NOT to a null yields NULL. Thus, the following two statements are not always equivalent: IF x > y THEN | IF NOT x > y THEN high := x; | high := y; ELSE | ELSE high := y; | high := x; END IF; | END IF; The sequence of statements in the ELSE clause is executed when the IF condition yields FALSE or NULL. If neither x nor y is null, both IF statements assign the same value to high. However, if either x or y is null, the first IF statement assigns the value of y to high, but the second IF statement assigns the value of x to high. Zero-Length Strings PL/SQL treats any zero-length string like a null. This includes values returned by character functions and Boolean expressions. For example, the following statements assign nulls to the target variables: null_string := TO_CHAR(''); zip_code := SUBSTR(address, 25, 0); valid := (name != ''); So, use the IS NULL operator to test for null strings, as follows: IF my_string IS NULL THEN ... Concatenation Operator The concatenation operator ignores null operands. For example, the expression 'apple' || NULL || NULL || 'sauce' returns the following value: 'applesauce' Functions If a null argument is passed to a built-in function, a null is returned except in the following cases. The function DECODE compares its first argument to one or more search expressions, which are paired with result expressions. Any search or result expression can be null. If a search is successful, the corresponding result is returned. In the following example, if the column rating is null, DECODE returns the value 1000: SELECT DECODE(rating, NULL, 1000, 'C', 2000, 'B', 4000, 'A', 5000) INTO credit_limit FROM accts WHERE acctno = my_acctno; The function NVL returns the value of its second argument if its first argument is null. In the example below, if hire_date is null, NVL returns the value of SYSDATE. Otherwise, NVL returns the value of hire_date: start_date := NVL(hire_date, SYSDATE); The function REPLACE returns the value of its first argument if its second argument is null, whether the optional third argument is present or not. For instance, after the assignment

new_string := REPLACE(old_string, NULL, my_string); the values of old_string and new_string are the same. If its third argument is null, REPLACE returns its first argument with every occurrence of its second argument removed. For example, after the assignments syllabified_name := 'Gold-i-locks'; name := REPLACE(syllabified_name, '-', NULL); the value of name is 'goldilocks' If its second and third arguments are null, REPLACE simply returns its first argument.

Built-In Functions PL/SQL provides many powerful functions to help you manipulate data. These built-in functions fall into the following categories: error reporting number character datatype conversion date object reference miscellaneous Table 2-3 shows the functions in each category. Except for the error-reporting functions SQLCODE and SQLERRM, you can use all the functions in SQL statements. Also, except for the object-reference functions DEREF, REF, and VALUE and the miscellaneous functions DECODE, DUMP, and VSIZE, you can use all the functions in procedural statements. Although the SQL aggregate functions (such as AVG and COUNT) and the SQL analytic functions (such as CORR and LAG) are not built into PL/SQL, you can use them in SQL statements (but not in procedural statements). Table 2-3 Built-In Functions Error SQLCOD E Number Character ABS ACOS ASCII CHR Conversion Date Obj Ref Misc

CHARTOROWID ADD_MONTHS CONVERT CURRENT_DATE

DEREF BFILENAME REF DECODE

Error SQLERR M

Number Character ASIN ATAN ATAN2 BITAN D CEIL COS COSH EXP FLOOR LN LOG MOD POWER ROUND SIGN SIN SINH SQRT TAN TANH TRUNC CONCAT INITCAP INSTR INSTRB LENGTH LENGTHB LOWER LPAD LTRIM NLS_INITCA P NLS_LOWER NLSSORT NLS_UPPER REPLACE RPAD RTRIM SOUNDEX SUBSTR SUBSTRB TRANSLATE TRIM UPPER

Conversion HEXTORAW RAWTOHEX ROWIDTOCHAR TO_BLOB TO_CHAR TO_CLOB TO_DATE TO_MULTI_BYT E TO_NCLOB TO_NUMBER TO_SINGLE_BYT E

Date

Obj Ref

Misc DUMP EMPTY_BLOB EMPTY_CLOB GREATEST LEAST NLS_CHARSET_DECL_LE N NLS_CHARSET_ID NLS_CHARSET_NAME NVL SYS_CONTEXT SYS_GUID UID USER USERENV VSIZE

CURRENT_TIMESTA VALU MP E DBTIMEZONE TREAT EXTRACT FROM_TZ LAST_DAY LOCALTIMESTAMP MONTHS_BETWEEN NEW_TIME NEXT_DAY NUMTODSINTERVAL NUMTOYMINTERVA L ROUND SESSIONTIMEZONE SYSDATE SYSTIMESTAMP TO_DSINTERVAL TO_TIMESTAMP TO_TIMESTAMP_LTZ TO_TIMESTAMP_TZ TO_YMINTERVAL TZ_OFFSET TRUNC

Das könnte Ihnen auch gefallen