tags:

views:

336

answers:

2

How do I iterate over a set of records in RPG(LE) with embedded SQL?

+6  A: 

Usually I'll create a cursor and fetch each record.

   //***********************************************************************
   // Main - Main Processing Routine
   begsr Main;

     exsr BldSqlStmt;

     if OpenSqlCursor() = SQL_SUCCESS;

       dow FetchNextRow() = SQL_SUCCESS;
         exsr ProcessRow;
       enddo;

       if sqlStt = SQL_NO_MORE_ROWS;
         CloseSqlCursor();
       endif;

     endif;

     CloseSqlCursor();

   endsr;    // Main
Mike Wills
+2  A: 

As Mike said, iterating over a cursor is the best solution. I would add to give slightly better performance, you might might want to fetch into an array to process in blocks rather than one record at a time.

Example:

  EXEC SQL                                                          
  OPEN order_history;                                             

  // Set the length                                                 
  len = %elem(results);                                             

  // Loop through all the results                                   
  dow (SqlState = Sql_Success);                                     
    EXEC SQL                                                        
      FETCH FROM order_history FOR :len ROWS INTO :results;         
    if (SQLER3 <> *zeros);                                                 
      for i = 1 to SQLER3 by 1;        
        // Load the output             
        eval-corr output = results(i); 
        // Do something 
      endfor;                          
    endif;                             
  enddo;

HTH, James R. Perkins

jamezp