• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Fetch a Variable Number of Records with SQL

    June 15, 2005 Hey, Ted

    If you want to use a variable for the number of rows to fetch in an SQL query, there is an alternative to the FETCH FIRST n ROWS technique you presented recently in Four Hundred Guru. Here is some code from an SQLRPGLE program shell I have used since V5R1. It uses a cursor instead of placing the number of rows directly into the SELECT statement.

    c/Exec SQL                                         
    c+ Fetch from C1 for :NbrRows rows into :RtnDtaSet 
    c/End-Exec
    

    –Rick

    Rick’s comment is in response to the last paragraph of the issue he refers to. His idea is to use a multi-row fetch, which requires that a structure of some sort (depending on the language) be defined to receive the data. In RPG, the data is placed into a multiple-occurrence data structure.

    Here’s another example that is a tad more complete. I have omitted the error-checking logic. The FETCH retrieves the number of rows specified by the first parameter.

    Fqsysprt   o    f  132        printer               
                                                        
    D CustData        ds                  occurs(12)    
    D  CustNumber                    6  0               
    D  CustName                     12                  
                                                        
    D Rows            s              3p 0               
    D Ndx             s              3p 0               
                                                        
    C     *entry        plist                           
    C                   parm                    Rows    
                                                        
    C/exec sql                                          
    C+           declare Customers cursor for           
    C+             select cusnum, lstnam || ' ' || init 
    C+               from qiws/qcustcdt                 
    C/end-exec                                          
    C/exec sql                                          
    C+           open Customers                         
    C/end-exec                                          
    C/exec sql                                          
    C+           fetch Customers for :Rows Rows         
    C+             into :CustData                       
    C/end-exec                                     
    C                   for       ndx = 1 to Rows  
    C     ndx           occur     CustData         
    C                   except    PLine            
    C                   endfor                     
    C/exec sql                                     
    C+           close Customers                   
    C/end-exec                                     
    C                   eval      *inlr = *on      
    Oqsysprt   e            pline       1          
    O                       Ndx           4        
    O                       CustNumber       +   1 
    O                       CustName         +   1 
    

    After the FETCH retrieves the number of rows indicated by the ROWS variable, a simple loop lists the retrieved records.

    –Ted

    RELATED STORY

    SQL Can Return One or a Few Records


    Share this:

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

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Now Solutions Brings HR App to DB2 Oracle Gives J.D. Edwards World Shops Something to Cheer About

    Leave a Reply Cancel reply

Volume 5, Number 23 -- June 15, 2005
THIS ISSUE
SPONSORED BY:

T.L. Ashford
iTera
WorksRight Software

Table of Contents

  • Query Active Directory from the iSeries
  • Fetch a Variable Number of Records with SQL
  • Admin Alert: A Quick Audit of Your IPL Parameters

Content archive

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

Recent Posts

  • Positive News From The Kyndryl Mainframe Modernization Report
  • NAViGATE, inPower 2025 On Tap for September 2025
  • Guru: WCA4i And Granite – Because You’ve Got Bigger Things To Build
  • As I See It: Digital Coup
  • IBM i PTF Guide, Volume 27, Number 37
  • AI Is Coming for ERP. How Will IBM i Respond?
  • The Power And Storage Price Wiggling Continues – Again
  • LaserVault Adds Multi-Path Support To ViTL
  • As I See It: Spacing Out
  • IBM i PTF Guide, Volume 27, Numbers 34, 35, And 36

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