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

2

7

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

Malady answered 23/9, 2008 at 11:30 Comment(0)
M
13

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 

I have added more detail to this answer in a post on my website.

Mouthful answered 23/9, 2008 at 13:29 Comment(1)
Hi @Mike Wills, I was interested to read more detail about this but the link to your website is broken.Spoilsport
I
8

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

Interdental answered 7/8, 2009 at 23:42 Comment(1)
I like this... I haven't done that before. I'll have to try it on large recordsets.Mouthful

© 2022 - 2024 — McMap. All rights reserved.