• 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
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    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

  • Inside The Encryption Key Management Changes In IBM i 7.6
  • FalconStor Moved To The Blue Lagoon, And Is Poised For Growth Because Of It
  • Guru: Claude’s SQL Tip
  • Astera Makes Extracting Legacy Report Data an AI Specialty
  • IBM i PTF Guide, Volume 28, Number 27
  • Welcoming The New IBM i Chief Architect And Other New Top Brass
  • A Deep Dive Into That Power S1112 Entry Power11 Server
  • Guru: Beyond Three-Part Naming – Running SQL Across Remote IBM i Systems
  • How IBM Bolstered IBM i Resilience In The Summer Tech Refreshes
  • IBM i PTF Guide, Volume 28, Number 26

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