• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: SQL PL, WHILE And REPEAT Loops

    January 18, 2017 Ted Holt

    In earlier editions of this august publication, I covered the SQL PL looping structures that I consider to be the most useful. Today I cover the remaining looping structures. I cover them for completeness, but also because what I consider to be most useful may not be what you consider to be most useful.

    To review, the FOR loop provides a simple way to process the rows of a result set. LOOP structure is versatile, providing the freedom to exit a loop from any point. The remaining loop structures are WHILE and REPEAT.

    The WHILE loop is a top-tested loop. It runs as long as a condition is true. If the governing condition is false when control first reaches the loop, the loop will not execute at all.

    The WHILE syntax is simple:

    WHILE condition DO body END WHILE

    The REPEAT loop is a bottom-tested loop. It runs until a condition is true. A REPEAT loop executes at least one time. The syntax of REPEAT is also simple.

    REPEAT body UNTIL condition END REPEAT

    RPG programmers will recognize these structures as the counterparts to the DOW and DOU op codes.

    To illustrate, let me return to a topic I wrote about awhile back, namely the need to convert a list into a temporary table. Here’s a routine that creates such a procedure.

    create or replace procedure BuildDepartmentList
       (in p_List   varchar(256))
    
    begin
       declare v_Department      dec     (  3);
       declare v_List            varchar (257);
       declare v_Pos             integer;
       declare v_Sep             varchar (  1)   default ',';
    
       set  v_List = trim(p_List) concat v_Sep;
    
       declare global temporary table DeptList
         ( Department     dec(2) )
       with replace;
    
       while v_List <> ' ' do
          set v_Pos = Locate (v_Sep, v_List);
          set v_Department = dec (substr ( v_List, 1, v_Pos - 1 ) );
          insert into session.DeptList values(v_Department);
          set v_List = substr (v_List, v_Pos + 1);
       end while;
    end
    

    And here’s how I call the procedure:

    call BuildDepartmentList ('3,4,9,11,69')
    

    When BuildDepartmentList starts running, p_List has the value <i>3,4,9,11,69</i>. The first SET copies the value of parameter p_List into v_List and adds a comma to the end.

    set  v_List = trim(p_List) concat v_Sep;
    

    This provides a work variable that the program can modify and ensures that the first SET within the WHILE loop always loads a positive value into v_Pos.

    The WHILE loop runs as long as v_List is not blank. After the first iteration:

    • v_Pos is 2
    • v_Department is 3
    • the DeptList table has a row with the value 3</li>
    • v_List has become 4,9,11,69

    Each iteration removes the first number from v_List and writes that number into the DeptList table. The loop ends after the fifth iteration, when v_List has a blank value.

    The body of the WHILE executes five times and DeptList has five rows (records). Now I can use the temporary table for row selection.

    select * 
      from GLXACTS
     where Dept in (select Department from session.DeptList)
    

    A WHILE loop is a WHILE loop is a WHILE loop, regardless of the syntax of a programming language. The SQL PL syntax for top-tested and bottom-tested loops is as simple as any.

     

    Ted Holt welcomes your comments and questions. Email him through the IT Jungle Contacts page.

     

    RELATED STORIES

    SQL PL–The LOOP Loop

    SQL PL–The FOR Loop

    Dynamic Lists In Static SQL Queries

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Tags:

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Mad Dog 21/21: As Ginni Bows Out What Will This Year Bring For IBM i?

    2 thoughts on “Guru: SQL PL, WHILE And REPEAT Loops”

    • Dan Lovell says:
      January 19, 2017 at 9:29 am

      Very good article! Another loop construct is the FOR x AS cursor_name CURSOR FOR SELECT COL1, COL2 FROM TABLEx WHERE COL1 = ‘ABC’ DO END FOR; In this construct for each row read processing is done.

      Reply
    • Ted Holt says:
      February 23, 2017 at 1:56 pm

      Thanks, Dan.

      See https://www.itjungle.com/2016/11/15/fhg111516-story02/ for an article about FOR loops.

      Reply

    Leave a ReplyCancel reply

TFH Volume: 27 Issue: 1

This Issue Sponsored By

  • Profound Logic Software
  • Chrono-Logic
  • WorksRight Software
  • System i Developer
  • Manta Technologies

Table of Contents

  • A Power Systems Wish List For The Year Ahead
  • What Will This Year Bring For IBM i?
  • Guru: SQL PL, WHILE And REPEAT Loops
  • Mad Dog 21/21: As Ginni Bows Out
  • IBM i Open Source Business Architect Lays Out A Plan

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • IBM i Delivers New Development Tooling With 2026 Fall Tech Refreshes
  • Big Blue Says AI Is Not Behind The Big Uptick In IBM i Security Alerts
  • Guru: Web Service With Pagination
  • The GenAI Revolution And Product Shortages Send IT Budgets Skyward
  • IBM i PTF Guide, Volume 28, Number 40
  • IBM i PTF Guide, Volume 28, Number 41
  • Bob Code Assistant Gets Better Understanding Of IBM i Applications
  • IBM i In The Cloud And The Case For Reversing Replication
  • Guru: From Hiking Boots To Long-Term Memory
  • Inside IBM’s New PowerHA Cloud Offering, Full System Manager

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle