IF-THEN v16

Syntax

IF boolean-expression THEN
  <statements>
END IF;

IF-THEN statements are the simplest form of IF. The statements between THEN and END IF are executed if the condition is TRUE. Otherwise, they are skipped.

Example

This example uses IF-THEN statement to test and display employees who have a commission:

DECLARE
    v_empno         emp.empno%TYPE;
    v_comm          emp.comm%TYPE;
    CURSOR emp_cursor IS SELECT empno, comm FROM emp;
BEGIN
    OPEN emp_cursor;
    DBMS_OUTPUT.PUT_LINE('EMPNO    COMM');
    DBMS_OUTPUT.PUT_LINE('-----    -------');
    LOOP
        FETCH emp_cursor INTO v_empno, v_comm;
        EXIT WHEN emp_cursor%NOTFOUND;
--
--  Test whether or not the employee gets a commission
--
        IF v_comm IS NOT NULL AND v_comm > 0 THEN
            DBMS_OUTPUT.PUT_LINE(v_empno || '  ' ||
            TO_CHAR(v_comm,'$99999.99'));
        END IF;
    END LOOP;
    CLOSE emp_cursor;
END;

The following is the output from this program:

Output
EMPNO    COMM
-----    -------
7499     $300.00
7521     $500.00
7654    $1400.00