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

 
 Home
 E-mail Us
 Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

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

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 
 

How to do a loop in Oracle SQL

Oracle Tips by Burleson Consulting
August 24,  2008


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'
                 End
               As
       RunProg1,
       --------------------------------------------------
               Case When Counter <= (Select Length(Ename)
                                       From Emp
                                      Where Rownum <2)
                    Then 'Looper'
                    Else ' '
                 End
               As
       RunProg2
  From GTT_CountHolder ;

Set Term On

@&Run_Code_Or_End

@&Run_Loop_Or_Stop

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.

code.sql

Column Character Format A9

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

Update GTT_CountHolder
   Set Counter = Counter + 1;

EndLoop.SQL

Truncate Table GTT_CountHolder ; Drop Table GTT_CountHolder ;

Lynn Tobias


 

 

��
 
 
 
 

 
 
 

 
Oracle performance tuning software 
 
oracle dba poster
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 -  2014

All rights reserved by Burleson

Oracle ? is the registered trademark of Oracle Corporation.