Newsletters   Subscriptions  Forums  Store   Career  Media Kit  About Us  Contact  Search   Home 
fhg
Volume 5, Number 23 -- June 15, 2005

Fetch a Variable Number of Records with SQL


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


Sponsored By
ITERA

Researching High Availability Solutions?

View the contents of this valuable iSeries high availability resource portal that includes HA white papers, archived webinars, case studies and more.

Learn essential information about iSeries high availability before you implement this powerful business-continuity technology.

Click here for instant access.


Technical Editors: Howard Arner, Joe Hertvik, Ted Holt,
Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.


THIS ISSUE
SPONSORED BY:

T.L. Ashford
iTera
WorksRight Software


Four Hundred Guru

BACK ISSUES

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


The Four Hundred
How Big Is the OS/400 Ecosystem?

IBM's BPMAC: A Small Group With Lots of Pull

HP, IBM and Unix, Windows Tied in the Server Market

As I See It: First Timers

Four Hundred Stuff
GST Speeds Backups with New Virtual Tape Library

Base One Update Brings Grids of Clusters

Oracle Supports Fusion Components with J.D. Edwards, iSeries

Now Solutions Brings HR App to DB2

Four Hundred Monitor


Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc. (formerly Midrange Server), 50 Park Terrace East, Suite 8F, New York, NY 10034
Privacy Statement