Oracle® OLAP DML Reference 10g Release 2 (10.2) Part Number B14346-02 |
|
|
View PDF |
The IF...THEN...ELSE command executes one or more statements in a program when a specified condition is met. Optionally, it also executes an alternative statement or group of statements when the condition is not met. You can use IF only within programs.
Note:
You can also use IF as a conditional operator in an expression. See "IF...THEN...ELSE Expressions"Syntax
IF boolean-expression
THEN statement1
[ELSE statement2]
Arguments
Any valid Boolean expression that returns either TRUE
or FALSE
.
Oracle OLAP executes the statement1 argument when the Boolean expression is TRUE
. The statement1 must be on the same line as THEN.
Oracle OLAP executes the statement2 argument when the Boolean expression is FALSE
. The statement2 must be on the same line as ELSE. When you omit the ELSE phrase, execution continues with the statement after the whole IF...THEN... statement in the program.
Notes
IF with DO
You can use an IF statement for conditional execution of two or more statements by following the THEN or ELSE (or both) keywords with a DO ... DOEND sequence. See Example 16-13, "Using IF...THEN...ELSE".
Single or Multiple Lines
When IF is used as an expression, the THEN and ELSE keywords must be on the same line as IF. When IF is used as a command, THEN and ELSE must be on separate lines.
Examples
Example 16-13 Using IF...THEN...ELSE
The following lines from a program illustrate the use of IF...THEN...ELSE.... When the Boolean expression ANY(DOLLARS LT 200000)
is TRUE
, the statements following THEN (statement group 1
) are executed. When the expression is FALSE
, the statements following ELSE (statement group 2
) are executed instead.
IF ANY(DOLLARS LT 200000) THEN DO ... " (statement group 1) DOEND ELSE DO ... "(statement group 2) DOEND
Example 16-14 Using IF as a Conditional Operator
In a program that produces a report, you would like to report a previous year's actual expenses or the current year's budget, depending on the year passed to the program as an argument. A conditional expression in a JOINCHARS function produces a heading with the word Actual or Budget. Another conditional expression selects the variable to report. The program would include the following lines.
ARGUMENT cur.year year LIMIT month TO year cur.year REPORT - HEADING JOINCHARS( 'Expenses: ' - IF cur.year LT 'Yr95' - THEN 'Actual FOR ' - ELSE 'Budget FOR ', - cur.year ) - IF cur.year LT 'Yr95' THEN actual ELSE budget