Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
50
Добавлен:
16.04.2013
Размер:
5.97 Mб
Скачать

PL/SQL Expressions and Comparisons

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, so you can test different variables or multiple conditions in a single WHEN clause. For example:

DECLARE

grade CHAR(1) := 'B'; appraisal VARCHAR2(120); id NUMBER := 8429862; attendance NUMBER := 150;

min_days CONSTANT NUMBER := 200;

FUNCTION attends_this_school(id NUMBER) RETURN BOOLEAN IS BEGIN RETURN TRUE; END;

BEGIN appraisal :=

CASE

WHEN attends_this_school(id) = FALSE THEN 'N/A - Student not enrolled'

--Have to put this condition early to detect

--good students with bad attendance

WHEN grade = 'F' OR attendance < min_days THEN 'Poor (poor performance or bad attendance)'

WHEN grade = 'A' THEN 'Excellent' WHEN grade = 'B' THEN 'Very Good' WHEN grade = 'C' THEN 'Good' WHEN grade = 'D' THEN 'Fair' ELSE 'No such grade'

END;

dbms_output.put_line('Result for student ' || id || ' is ' || appraisal);

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.

Fundamentals of the PL/SQL Language 2-25

PL/SQL Expressions and Comparisons

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.

DECLARE

x NUMBER := 5;

y NUMBER := NULL; BEGIN

IF x != y THEN -- yields NULL, not TRUE dbms_output.put_line('x != y'); -- not executed

ELSIF x = y THEN -- also yields NULL dbms_output.put_line('x = y');

ELSE

dbms_output.put_line('Can''t tell if x and y are equal or not...'); END IF;

END;

/

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.

DECLARE

a NUMBER := NULL; b NUMBER := NULL;

BEGIN

IF a = b THEN -- yields NULL, not TRUE dbms_output.put_line('a = b'); -- not executed

ELSIF a != b THEN -- yields NULL, not TRUE dbms_output.put_line('a != b'); -- not executed

ELSE

dbms_output.put_line('Can''t tell if two NULLs are equal'); END IF;

END;

/

NULLs and the 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.

NULLs and 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:

DECLARE

2-26 PL/SQL User's Guide and Reference

PL/SQL Expressions and Comparisons

null_string VARCHAR2(80) := TO_CHAR(''); address VARCHAR2(80);

zip_code VARCHAR2(80) := SUBSTR(address, 25, 0); name VARCHAR2(80);

valid BOOLEAN := (name != ''); BEGIN

NULL;

END;

/

Use the IS NULL operator to test for null strings, as follows:

IF my_string IS NULL THEN ...

NULLs and the Concatenation Operator

The concatenation operator ignores null operands. For example, the expression

'apple' || NULL || NULL || 'sauce'

returns the following value:

'applesauce'

NULLs as Arguments to Built-In 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:

DECLARE

the_manager VARCHAR2(40); name employees.last_name%TYPE;

BEGIN

--NULL is a valid argument to DECODE. In this case, manager_id is null

--and the DECODE function returns 'nobody'.

SELECT DECODE(manager_id, NULL, 'nobody', 'somebody'), last_name INTO the_manager, name FROM employees WHERE employee_id = 100;

dbms_output.put_line(name || ' is managed by ' || the_manager); END;

/

The function NVL returns the value of its second argument if its first argument is null. In the following example, if the column specified in the query is null, the function returns the value -1 to signify a non-existent employee in the output:

DECLARE

the_manager employees.manager_id%TYPE; name employees.last_name%TYPE;

BEGIN

--NULL is a valid argument to NVL. In this case, manager_id is null

--and the NVL function returns -1. SELECT NVL(manager_id, -1), last_name

INTO the_manager, name FROM employees WHERE employee_id = 100; dbms_output.put_line(name || ' is managed by employee #' || the_manager);

END;

/

Fundamentals of the PL/SQL Language 2-27

Соседние файлы в папке Oracle 10g