Previous |
Next |
The IF
statement either executes or skips a sequence of statements, depending on the value of a Boolean expression.
The IF
statement has this syntax:
IF boolean_expression THEN statement [, statement ] [ ELSEIF boolean_expression THEN statement [, statement ] ]... [ ELSE statement [, statement ] ] END IF;
Suppose that your company evaluates employees twice a year in the first 10 years of employment, but only once a year afterward. You want a function that returns the evaluation frequency for an employee. You can use an IF
statement to determine the return value of the function, as in the following example.
Add the eval_frequency
function to the body of the EMP_EVAL
package, but not to the specification. Because it is not in the specification, eval_frequency
is local to the package—it can be invoked only by other subprograms in the package, not from outside the package.
IF Statement that Determines Return Value of Function
FUNCTION eval_frequency (emp_id IN EMPLOYEES.EMPLOYEE_ID%TYPE) RETURN PLS_INTEGER AS h_date EMPLOYEES.HIRE_DATE%TYPE; today EMPLOYEES.HIRE_DATE%TYPE; eval_freq PLS_INTEGER; BEGIN SELECT SYSDATE INTO today FROM DUAL; SELECT HIRE_DATE INTO h_date FROM EMPLOYEES WHERE EMPLOYEE_ID = emp_id; IF ((hire_date + (INTERVAL '120' MONTH)) < today) THEN eval_freq := 1; ELSE eval_freq := 2; END IF; RETURN eval_freq; END eval_frequency;
Oracle Database PL/SQL Language Reference for the syntax of the IF
statement
Oracle Database PL/SQL Language Reference for more information about using the IF
statement