Press "Enter" to skip to content

Oracle FOR LOOP SELECT Statement Example

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;
/

Output:

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.

See also:

Vinish Kapoor

Hi, I am a full stack developer and writing about development. I document everything I learn and help thousands of people. foxinfotech.in is created, written, and maintained by me; it is built on WordPress, and hosted by Bluehost. Connect with me on Facebook, Twitter, GitHub, and get notifications for new posts.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *