In this tutorial, I am giving an Oracle FOR LOOP SELECT statement example. You can use SELECT statement inside FOR LOOP in Oracle to loop through SELECT results using PL/SQL.
FOR LOOP IN SELECT Syntax
FOR cursor_variable IN (select_statement) LOOP -- commands to execute END LOOP;
Oracle FOR LOOP SELECT Statement Example
In the following example, it will loop through all the records from EMP table where department is equal to 10 and will print the employees salary.
SET SERVEROUTPUT ON; BEGIN FOR c IN (SELECT EMPNO, ENAME, SAL FROM emp WHERE deptno = 10) LOOP DBMS_OUTPUT.PUT_LINE ( 'Salary for the employee ' || c.ename || ' is: ' || c.sal); END LOOP; END; /
Salary for the employee CLARK is: 2450 Salary for the employee KING is: 5000 Salary for the employee MILLER is: 1300 PL/SQL procedure successfully completed.