Oracle pl/SQL's "art appreciation" from if to then"
In oracle development, the conditional expressions between if and then are full of "art" and require us to discover and appreciate them. Appreciation 1: What is the final result of a three-value logic expression if it contains a null value? For example, 2 <null. Null is a black box, and oracle does not know what is missing. The only answer that can be given is "I don't know ". This is the core of the Three-value Logic. Possible results can be true, false, or null. Example: [SQL] IF salary> 4000 THEN give_bonus (employee_id); END IF; null value in the Boolean expression www.2cto.com, the result of the entire expression is also null. That is, if salary is null, then salary> 4000 is null. Some operators (is null/is not null) or functions (nvl () are designed to specifically process null values and control the results to binary logic. For example, [SQL] IF salary> 4000 or salary IS NULL THEN give_bonus (employee_id); END IF; in this way, employees with missing salaries can also receive additional allowances. Therefore, for each variable in each Boolean expression, consider the consequences of null. Appreciation 2: the short-circuit behavior pl/SQL uses the short-circuit evaluate method, that is, pl/SQL does not need to evaluate all expressions in an if statement. For example, when the expression in the IF statement below is evaluated, as long as the result of condition1 is false or null, pl/SQL will stop evaluate the entire expression, give control to the ELSE branch. [SQL] IF condition1 AND condition2 THEN... ELSE... end if; www.2cto.com is similar to AND. Using the OR operator, as long as condition1 is true, pl/SQL immediately executes the THEN branch. If a condition is expensive, such as consuming a large amount of cpu and memory resources, short circuit is very useful. In this case, make sure that the expensive condition is placed at the end of the entire condition set: [SQL] IF low_cpu_condition AND high_cpu_condition THEN... ELSE... end if; Appreciation 3: Boolean variables are more convenient to use as signs. If you want to do this, remember that you can directly assign a Boolean expression to a Boolean variable. Use the following code: