fhg
Volume 6, Number 13 -- March 29, 2006

How Many Rows Did SQL Fetch?

Published: 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?



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
E-mail: software@worksright.com
Web site: www.worksright.com



Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, 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.

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

 


 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement