SQL$HELP72.HLB  —  LOOP Control
    Allows the repetitive execution of one or more SQL statements in
    a compound statement.

    See also the FOR, REPEAT and WHILE statements.

1  –  Environment

    You can use the LOOP control statement only within a compound
    statement:

    o  In interactive SQL

    o  Embedded in host language programs to be precompiled

    o  As part of a procedure in an SQL module

    o  In dynamic SQL as a statement to be dynamically executed

2  –  Format

  (B)0loop-statement =                                    
                                                      
  qqqwqqqqqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqk
     mq> <beginning-label> : j                   x
     lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj
     mq> LOOP qqwq> compound-use-statement qwqqqk     
                mqqqqqqqqqqqqq<qqqqqqqqqqqqqj   x     
     lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj     
     mq> END LOOP qqwqqqqqqqqqqqqqqqqqqwq>            
                    mq> <ending-label> j              
                                                      

  (B)0compound-use-statement =                   
                                             
  qqqqwq> call-statement qqqqqqqqqqqqqqwqq> ;
      tq> commit-statement qqqqqqqqqqqqu     
      tq> control-statement qqqqqqqqqqqu     
      tq> delete-statement qqqqqqqqqqqqu     
      tq> get-diagnostics-statement qqqu     
      tq> insert-statement qqqqqqqqqqqqu     
      tq> lock-table-statement qqqqqqqqu     
      tq> rollback-statement qqqqqqqqqqu     
      tq> set-transaction-statement qqqu     
      tq> singleton-select-statement qqu     
      tq> start-transaction-statement qu
      tq> trace-statement qqqqqqqqqqqqqu     
      mq> update-statement qqqqqqqqqqqqj     

3  –  Arguments

3.1  –  beginning-label

    Assigns a name to a control loop. A beginning label used with the
    LEAVE statement lets you perform a controlled exit from a loop. A
    named loop is called a labeled loop statement. If you include an
    ending label, it must be identical to its corresponding beginning
    label. A beginning label must be unique within the procedure in
    which the label is contained.

3.2  –  compound-use-statement

    Identifies the SQL statements allowed in a compound statement
    block. See the Compound_Statement HELP topic for the list of
    valid statements.

3.3  –  END_LOOP

    Marks the end of a control loop. If you choose to include the
    optional ending label, it must match exactly its corresponding
    beginning label. An ending label must be unique within the
    procedure in which the label is contained.

    The optional end-label argument makes multistatement procedures
    easier to read, especially in very complex multistatement
    procedure blocks.

3.4  –  LOOP

    Marks the start of a control loop. A LOOP statement enables you
    to execute the associated sequence of SQL statements called a
    compound statement. After SQL executes the statements within the
    loop, control returns to the LOOP statement at the top of the
    loop for subsequent statement execution. Looping occurs until
    SQL encounters an error exception or executes a LEAVE statement.
    In either case, SQL passes control out of the LOOP block to the
    statement immediately after the LOOP statement.

4  –  Examples

    Example 1: Executing a loop statement

    SQL> create table ENROLLMENTS
    cont>     (last_name          char(20),
    cont>      first_name         char(10),
    cont>      middle_initial     char,
    cont>      class_name         char(10));
    SQL>
    SQL> begin
    cont> declare :n integer default 5;
    cont> loop
    cont>     insert into ENROLLMENTS
    cont>       values ('Jones', 'Robert', 'A',
    cont>               'Class ' || CAST(:n as char(1)));
    cont>     set :n = :n - 1;
    cont>     if :n <= 0 then
    cont>         leave;
    cont>     end if;
    cont> end loop;
    cont> end;
    SQL>
    SQL> select * from ENROLLMENTS;
     LAST_NAME              FIRST_NAME   MIDDLE_INITIAL   CLASS_NAME
     Jones                  Robert       A                Class 5
     Jones                  Robert       A                Class 4
     Jones                  Robert       A                Class 3
     Jones                  Robert       A                Class 2
     Jones                  Robert       A                Class 1
    5 rows selected
    SQL>
Close Help