• 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:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    ARCAD Software

    DevSecOps & Peer Review – The Power of Automation

    In today’s fast-paced development environments, security can no longer be an afterthought. This session will explore how DevSecOps brings security into every phase of the DevOps lifecycle—early, consistently, and effectively.

    In this session, you’ll discover:

    • What DevSecOps is and why it matters?
    • Learn how to formalize your security concerns into a repeatable process
    • Discover the power of automation through pull requests, approval workflows, segregation of duties, peer review, and more—ensuring your data and production environments are protected without slowing down delivery.

    Whether you’re just getting started or looking to enhance your practices, this session will provide actionable insights to strengthen your security posture through automation and team alignment to bring consistency to the process.

    Watch Now!

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • 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 Reply Cancel 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

  • Power Systems Grows Nicely In Q3, Looks To Grow For All 2025, Too
  • Beta Of MCP Server Opens Up IBM i For Agentic AI
  • Sundry IBM i And Power Stack Announcements For Your Consideration
  • Please Take The IBM i Marketplace Survey
  • IBM i PTF Guide, Volume 27, Number 43
  • IBM Pulls The Curtain Back A Smidge On Project Bob
  • IBM Just Killed Merlin. Here’s Why
  • Guru: Playing Sounds From An RPG Program
  • A Bit More Insight Into IBM’s “Spyre” AI Accelerator For Power
  • IBM i PTF Guide, Volume 27, Number 42

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