While Loop Example

Description: Basic WHILE loop skeleton code.

DECLARE
    salary          emp.sal%TYPE;
    mgr_num         emp.mgr%TYPE;
    last_name       emp.ename%TYPE;
    starting_empno  CONSTANT NUMBER(4) := 7902;
BEGIN
    SELECT sal, mgr INTO salary, mgr_num FROM emp
        WHERE empno = starting_empno;
    WHILE salary < 4000 LOOP
        SELECT sal, mgr, ename INTO salary, mgr_num, last_name
            FROM emp
            WHERE empno = mgr_num;
    END LOOP;
 
    INSERT INTO temp VALUES (NULL, salary, last_name);
    COMMIT;
END;

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.