Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 E-mail Us
 Oracle Articles
New Oracle Articles

 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog

 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Oracle Support

 SQL Tuning

 Oracle UNIX
 Oracle Linux
 Remote s
 Remote plans
 Application Server

 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S


 Consulting Staff
 Consulting Prices
 Help Wanted!


 Oracle Posters
 Oracle Books

 Oracle Scripts

Don Burleson Blog 







How to do a loop in Oracle SQL

Oracle Database Tips by Donald BurlesonAugust 24,  2015

Question:  I have a need to do a loop in my SQL without using PL/SQL.  I understand that PL/SQL was create for process logic like IF and LOOPING, but can Oracle SQL support loop constructs directly?  I want to use Oracle SQL to perform a FOR loop.

Answer:  SQL was first developed as a "Structured query Language", although you could argue that SQL is not structured, for only for queries, and not a language since SQL is normally embedded into a host language like C or PL/SQL.  However, SQL is expending, and Oracle SQL now indirectly supports many of the functions of a procedural language such as branching (see the CASE operator) and rudimentary loop constructs.

In this example below, we see Oracle SQL with a FOR loop, embedded into the xmltable stored procedure.  This SQL also materializes a intermediate result set by using the SQL WITH clause:

SQL> with t as
  select date '2008-06-01' d1, date '2008-06-10' d2 from dual
select d1 + i days
  from t, xmltable('for $i in 0 to xs:int(D) return $i' passing xmlelement(d, d2-d1)
                                                        columns i integer path '.')

So, while Oracle SQL does not directly support while loops of for loops, there is extended syntax for looping within some stored procedures that are embedded into Oracle SQL.

Reader comments:

I always enjoy your site. I saw someone request a loop in SQL so I tried your SQL loop code.  I decided to make my own version of SQL looping (which is a lot longer than yours, but of course, I can understand it! :)

Set Term &OnOff

Column RunProg1 New_Value Run_Code_Or_End
column RunProg2 New_Value Run_Loop_Or_Stop

Select         Case When Counter <= (Select Length(Ename)
                                       From Emp
                                      Where Rownum <2)
                    Then 'Code'
                    Else 'EndLoop'
               Case When Counter <= (Select Length(Ename)
                                       From Emp
                                      Where Rownum <2)
                    Then 'Looper'
                    Else ' '
  From GTT_CountHolder ;

Set Term On



Code is the actual code that we want to run multiple times. It runs the query and then updates the Counter in the temp table.


Column Character Format A9

Select Ename ,
       -- Show position
                (Select Counter
                  from GTT_CountHolder )
       -- Show character
       -- Substr(field,position,length)
                Substr(Ename                       /* Field    */ 
                      , (Select Counter
                           from GTT_CountHolder )  /* Position */ 
                      ,1)                          /* Length   */
  From Emp
 Where Rownum < 2;

Update GTT_CountHolder
   Set Counter = Counter + 1;


Truncate Table GTT_CountHolder ; Drop Table GTT_CountHolder ;

Lynn Tobias



Oracle Training at Sea
oracle dba poster

Follow us on Twitter 
Oracle performance tuning software 
Oracle Linux poster


Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.