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

PL/SQL Expressions and Comparisons

Assigning Boolean Values

Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable. You can assign these literal values, or expressions such as comparisons using relational operators.

DECLARE

done BOOLEAN; -- DONE is initially NULL counter NUMBER := 0;

BEGIN

done := FALSE; -- Assign a literal value

WHILE done != TRUE -- Compare to a literal value LOOP

counter := counter + 1;

done := (counter > 500); -- If counter > 500, DONE = TRUE END LOOP;

END;

/

Assigning a SQL Query Result to a PL/SQL Variable

You can use the SELECT statement to have Oracle assign values to a variable. For each item in the select list, there must be a corresponding, type-compatible variable in the INTO list. For example:

DECLARE

 

emp_id

employees.employee_id%TYPE := 100;

emp_name

employees.last_name%TYPE;

wages

NUMBER(7,2);

BEGIN

SELECT last_name, salary + (salary * nvl(commission_pct,0)) INTO emp_name, wages FROM employees

WHERE employee_id = emp_id;

dbms_output.put_line('Employee ' || emp_name || ' might make ' || wages); END;

/

Because SQL does not have a Boolean type, you cannot select column values into a Boolean variable.

PL/SQL Expressions and Comparisons

Expressions are constructed using operands and operators. An operand is a variable, constant, literal, or function call that contributes a value to an expression. An example of a simple arithmetic expression follows:

-X / 2 + 3

Unary operators such as the negation operator (-) operate on one operand; binary operators such as the division operator (/) operate on two operands. PL/SQL has no ternary operators.

The simplest expressions consist of a single variable, which yields a value directly. PL/SQL evaluates an expression by combining the values of the operands in ways specified by the operators. An expression always returns a single value. PL/SQL determines the datatype of this value by examining the expression and the context in which it appears.

Fundamentals of the PL/SQL Language 2-17

PL/SQL Expressions and Comparisons

Operator Precedence

The operations within an expression are done in a particular order depending on their precedence (priority). Table 2–1 shows the default order of operations from first to last (top to bottom).

Table 2–1 Order of Operations

Operator

Operation

 

 

**

exponentiation

+, -

identity, negation

*, /

multiplication, division

+, -, ||

addition, subtraction, concatenation

=, <, >, <=, >=, <>, !=, ~=, ^=, comparison

IS NULL, LIKE, BETWEEN, IN

 

NOT

logical negation

AND

conjunction

OR

inclusion

 

 

Operators with higher precedence are applied first. In the example below, both expressions yield 8 because division has a higher precedence than addition. Operators with the same precedence are applied in no particular order.

5 + 12 / 4

12 / 4 + 5

You can use parentheses to control the order of evaluation. For example, the following expression yields 7, not 11, because parentheses override the default operator precedence:

(8 + 6) / 2

In the next example, the subtraction is done before the division because the most deeply nested subexpression is always evaluated first:

100 + (20 / 5 + (7 - 3))

The following example shows that you can always use parentheses to improve readability, even when they are not needed:

(salary * 0.05) + (commission * 0.25)

Logical Operators

The logical operators AND, OR, and NOT follow the tri-state logic shown in Table 2–2. AND and OR are binary operators; NOT is a unary operator.

Table 2–2 Logic Truth Table

x

y

x AND y

x OR y

NOT x

 

 

 

 

 

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

FALSE

TRUE

FALSE

TRUE

NULL

NULL

TRUE

FALSE

FALSE

TRUE

FALSE

TRUE

TRUE

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

PL/SQL Expressions and Comparisons

Table 2–2

(Cont.) Logic Truth Table

 

 

 

 

 

 

x

y

x AND y

x OR y

NOT x

 

 

 

 

 

FALSE

FALSE

FALSE

FALSE

TRUE

FALSE

NULL

FALSE

NULL

TRUE

NULL

TRUE

NULL

TRUE

NULL

NULL

FALSE

FALSE

NULL

NULL

NULL

NULL

NULL

NULL

NULL

 

 

 

 

 

As the truth table shows, AND returns TRUE only if both its operands are true. On the other hand, OR returns TRUE if either of its operands is true. NOT returns the opposite value (logical negation) of its operand. For example, NOT TRUE returns FALSE.

NOT NULL returns NULL, because nulls are indeterminate. Be careful to avoid unexpected results in expressions involving nulls; see "Handling Null Values in Comparisons and Conditional Statements" on page 2-25.

Order of Evaluation

When you do not use parentheses to specify the order of evaluation, operator precedence determines the order. Compare the following expressions:

NOT (valid AND done)

|

NOT valid AND done

If the Boolean variables valid and done have the value FALSE, the first expression yields TRUE. However, the second expression yields FALSE because NOT has a higher precedence than AND. Therefore, the second expression is equivalent to:

(NOT valid) AND done

In the following example, notice that when valid has the value FALSE, the whole expression yields FALSE regardless of the value of done:

valid AND done

Likewise, in the next example, when valid has the value TRUE, the whole expression yields TRUE regardless of the value of done:

valid OR done

Short-Circuit Evaluation

When evaluating a logical expression, PL/SQL uses short-circuit evaluation. That is, PL/SQL stops evaluating the expression as soon as the result can be determined. This lets you write expressions that might otherwise cause an error. Consider the following OR expression:

DECLARE

on_hand INTEGER := 0; on_order INTEGER := 100;

BEGIN

--Does not cause divide-by-zero error; evaluation stops after 1st expr. IF (on_hand = 0) OR ((on_order / on_hand) < 5) THEN

dbms_output.put_line('There are no more widgets left!'); END IF;

END;

/

Fundamentals of the PL/SQL Language 2-19

PL/SQL Expressions and Comparisons

When the value of on_hand is zero, the left operand yields TRUE, so PL/SQL does not evaluate the right operand. If PL/SQL evaluated both operands before applying the OR operator, the right operand would cause a division by zero error.

Comparison Operators

Comparison operators compare one expression to another. The result is always true, false, or null. Typically, you use comparison operators in conditional control statements and in the WHERE clause of SQL data manipulation statements. Here are some examples of comparisons for different types:

DECLARE

PROCEDURE assert(assertion VARCHAR2, truth BOOLEAN) IS

BEGIN

IF truth IS NULL THEN

dbms_output.put_line('Assertion ' || assertion || ' is unknown (NULL)'); ELSIF truth = TRUE THEN

dbms_output.put_line('Assertion ' || assertion || ' is TRUE'); ELSE

dbms_output.put_line('Assertion ' || assertion || ' is FALSE'); END IF;

END; BEGIN

assert('2 + 2 = 4', 2 + 2 = 4); assert('10 > 1', 10 > 1); assert('10 <= 1', 10 <= 1);

assert('5 BETWEEN 1 AND 10', 5 BETWEEN 1 AND 10); assert('NULL != 0', NULL != 0);

assert('3 IN (1,3,5)', 3 IN (1,3,5)); assert('''A'' < ''Z''', 'A' < 'Z');

assert('''baseball'' LIKE ''%all%''', 'baseball' LIKE '%all%'); assert('''suit'' || ''case'' = ''suitcase''', 'suit' || 'case' = 'suitcase');

END;

/

Relational Operators

Operator Meaning

=equal to

<>, !=, ~=, ^= not equal to

<less than

>greater than

<=

less than or equal to

>=

greater than or equal to

 

 

IS NULL Operator

The IS NULL operator returns the Boolean value TRUE if its operand is null or FALSE if it is not null. Comparisons involving nulls always yield NULL. Test whether a value is null as follows:

IF variable IS NULL THEN ...

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

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