• 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:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    ARCAD Software

    Embrace VS Code for IBM i Development

    The IBM i development landscape is evolving with modern tools that enhance efficiency and collaboration. Ready to make the move to VS Code for IBM i?

    Watch this webinar where we showcase how VS Code can serve as a powerful editor for native IBM i code and explore the essential extensions that make it possible.

    In this session, you’ll discover:

    • How ARCAD’s integration with VS Code provides deep metadata insights, allowing developers to assess the impact of their changes upfront.
    • The role of Git in enabling seamless collaboration between developers using tools like SEU, RDi, and VS Code.
    • Powerful extensions for code quality, security, impact analysis, smart build, and automated RPG conversion to Free Form.
    • How non-IBM i developers can now contribute to IBM i projects without prior knowledge of its specifics, while ensuring full control over their changes.

    The future of IBM i development is here. Let ARCAD be your guide!

    Watch the replay now!

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • 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

    Sam Palmisano Invites Me to India IBM Hints at Triple Redundancy in Power6

    Leave a Reply Cancel 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

  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19
  • IBM Unveils Manzan, A New Open Source Event Monitor For IBM i
  • Say Goodbye To Downtime: Update Your Database Without Taking Your Business Offline
  • i-Rays Brings Observability To IBM i Performance Problems
  • Another Non-TR “Technology Refresh” Happens With IBM i TR6
  • IBM i PTF Guide, Volume 27, Number 18

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