• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • How Many Rows Did SQL Fetch?

    March 29, 2006 Hey, Ted

    I am using SQL to load a subfile. Since there are 16 subfile records to a page, I fetch 16 database records into a data structure. However, the fetch does not always return 16 rows. Sometimes it returns fewer. How can I tell how many data structure occurrences were fetched?

    –Lynn

    You will need to check the SQLER3 field of the SQL Communications Area (SQLCA). I wrote about this field earlier in Four Hundred Guru. See “How Many Records Did SQL Delete?”

    Here’s an example I threw together that you can start from. It fetches seven records at a time from file QCUSTCDT, which you should be able to find in library QIWS on your system.

    Fqsysprt   o    f  132        printer                                 
                                                                          
    D CustRec       e ds                  extname(QCUSTCDT) occurs(7) inz 
    D RecCt           s              3p 0                                 
    D Index           s             10i 0                                 
    D FetchCt         s              3p 0                                 
                                                                          
    C                   eval      *inlr = *on                             
    C/exec sql                                                            
    C+   declare c1 cursor for                                            
    C+      select *                                                      
    C+        from qcustcdt                                               
    C/end-exec                                                            
    C/exec sql                                                            
    C+   open c1                                                          
    C/end-exec                                                            
    C                   dow       '1'                                     
    C/exec sql                                                            
    C+   fetch c1                                                         
    C+     for 7 rows                                                     
    C+    into :CustRec                                                   
    C/end-exec                                                            
    C                   if        sqlstt <> *zeros
    C                   leave                        
    C                   endif                        
    C                   eval      FetchCt += 1       
    C                   for       index = 1 to SqlEr3
    C                   eval      RecCt += 1         
    C     index         occur     CustRec            
    C                   except    pline              
    C                   endfor                       
    C                   enddo                        
    C/exec sql                                       
    C+   close c1                                    
    C/end-exec                                       
    C                   return                       
    Oqsysprt   e            pline       1            
    O                       FetchCt       4          
    O                       RecCt         4  +0001   
    O                       SQLEr3           +0001   
    O                       cusnum           +0001   
    O                       lstnam           +0001   
    O                       init             +0001
    

    The program has two loops. The outer loop fetches up to seven rows into a multiple-occurrence data structure. The inner loop processes the data that was placed into the data structure. Notice that the SQLER3 field serves to limit the number of iterations of the inner loop.

    Here’s the output I got when I ran the program. The first column counts the fetches. Notice that the program had to fetch twice to access 12 rows. The second column numbers the records. The third column is SQLER3. It shows that the first fetch retrieved seven rows, whereas the second fetch retrieved five. The remaining columns come from the database file.

    1   1 000000007 938472 Henning  G K
    1   2 000000007 839283 Jones    B D
    1   3 000000007 392859 Vine     S S
    1   4 000000007 938485 Johnson  J A
    1   5 000000007 397267 Tyron    W E
    1   6 000000007 389572 Stevens  K L
    1   7 000000007 846283 Alison   J S
    2   8 000000005 475938 Doe      J W
    2   9 000000005 693829 Thomas   A N
    2  10 000000005 593029 Williams E D
    2  11 000000005 192837 Lee      F L
    2  12 000000005 583990 Abraham  M T
    

    –Ted

    RELATED STORY

    How Many Records Did SQL Delete?

    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
    CloudSAFE

    CloudSAFE – for secure, scalable hosting and managed services for IBM i environments, delivering high availability, 24×7 monitoring, backup, recovery, and expert support to modernize operations, reduce risk, and ensure always-on performance reliably.

    LEARN MORE

    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

    Sponsored Links

    SoftLanding Systems:  TurnOver Change Management for a more productive WDSc environment
    Computer Keyes:  Rapidly convert *SCS printer files into black and white or full color PDF documents
    Maximum Availability:  Secure, cost-effective, real-time iSeries replication software solutions

    What’s New in V5R4 COBOL? Setting Up User Profiles Without Passwords

    Leave a ReplyCancel reply

Volume 6, Number 13 -- March 29, 2006
THIS ISSUE SPONSORED BY:

Advanced Systems Concepts
WorksRight Software
Bug Busters Software Engineering

Table of Contents

  • What’s New in V5R4 COBOL?
  • How Many Rows Did SQL Fetch?
  • Setting Up User Profiles Without Passwords

Content archive

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

Recent Posts

  • IBM’s Bob AI Assistant Can Now Live In Your Datacenter
  • Start Where the Data Lives: Best Practices for Taking IBM i Into AI
  • Guru: A Modernization Experiment Using IBM Bob 2.0
  • How Are IBM i Shops Using AI? Fortra and COMMON Want to Know
  • IBM i PTF Guide, Volume 28, Number 34
  • It Is Time Once Again To Take The IBM i Marketplace Survey
  • PHP’s Evolution On The IBM i Platform Continues
  • Protecting IBM i Data For The AI Revolution
  • Guru: Two More Small But Handy RPG Enhancements: LIKE(*EXT) And %LOOKUPNE
  • IBM i Integration Gives Costco Shoppers Real-Time Inventory

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