Nested Loops: Loop Through Tables

Description: This code demonstrates the use of cursors to loop through data within tables.

DECLARE
   CURSOR dept_cur IS
   SELECT deptno
     FROM dept
    ORDER BY deptno;
 
   -- Employee cursor all employees for a dept number
   CURSOR emp_cur (v_dept_no DEPT.DEPTNO%TYPE) IS
   SELECT ename
     FROM emp
    WHERE deptno = v_dept_no;
BEGIN
   FOR dept_rec IN dept_cur LOOP
      DBMS_OUTPUT.put_line('Employees in Department '||TO_CHAR(dept_rec.deptno));
 
      FOR emp_rec IN emp_cur(dept_rec.deptno) LOOP
         DBMS_OUTPUT.put_line('...Employee is '||emp_rec.ename);
      END LOOP;
 
  END LOOP;
END;
/

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.