Fetch to datastructure in RPG/400

370 pts.
Tags:
C+
Data structures
RPG
RPG/400
I have one rpg pgm in which I am creating a cursor on select to slect some records.After then I am opening cursor and fetching its data to an external data structure.Now my question is whether the external data structure takes only one record(first record) or it will have all records selected through select ststament. Example code: ITXREC E DSFKITSAVE ...... ....... C/EXEC SQL C+ declare HIST cursor for C+ select *from FKITSAVE C+ where TSCO=:UPINT and TSTTYP=:TYP. ............ fetch HIST into :TXREC
ASKED: July 11, 2008  12:50 PM
UPDATED: May 4, 2010  7:20 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

Hi,

This should only retrieve one record into your data structure (your data structure is defined as single occurrence, so cannot contain more than one record worth of data anyway), but you should specify first, next or last on your FETCH to be sure of which record the fetch will retrieve.

Regards,

Martin Gilbert.

========================================================

A multi-row FETCH or INSERT can use a multi-occurrence data structure or an array data structure. It’s not necessary to loop to FETCH multiple rows. Here’s some RPG coding to FETCH 10 rows at once:<pre>
DDEPARTMENT DS OCCURS(10)
D DEPTNO 01 03A
D DEPTNM 04 32A
D MGRNO 33 38A
D ADMRD 39 41A
DIND_ARRAY DS OCCURS(10)
D INDS 4B 0 DIM(4)

C/EXEC SQL
C+ DECLARE C1 CURSOR FOR
C+ SELECT *
C+ FROM CORPDATA.DEPARTMENT
C/END-EXEC

C/EXEC SQL
C+ FETCH C1 FOR 10 ROWS
C+ INTO :DEPARTMENT:IND_ARRAY
C/END-EXEC</pre>

Tom

Discuss This Question: 3  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Alicsc
    Hi, Can you give me one example of FETCH statment to fetch next and last record
    370 pointsBadges:
    report
  • Sloopy
    If you want to select more than one record through the SQL SELECT, you need a loop :
    C/EXEC SQL DECLARE C1 CURSOR FOR              
    C+         SELECT   SUM(NDTCFT),              
    C+                  SUM(NDTCF2), SUM(NDTCL2), 
    C+                  SUM(NDTCF4), SUM(NDTCL4), 
    C+                  NDSTPRCC, NDPOD           
    C+         FROM     VDNDDOC                   
    C+         WHERE    NDSTPRCC >= :DWSTPF AND   
    C+                  NDSTPRCC <= :DWSTPT AND   
    C+                  NDSH      = :DWP101       
    C+         GROUP BY NDSTPRCC, NDPOD           
    C+         ORDER BY NDSTPRCC, NDPOD           
    C/END-EXEC                                    
                                                  
    C/EXEC SQL                                    
    C+         OPEN C1                            
    C/END-EXEC                                    
    
    Then :
    C/EXEC SQL WHENEVER NOT FOUND GO TO FINISHED                       
    C/END-EXEC                                                         
                                                                       
    C                   DoU       SQLCOD      = 100                    
                                                                       
    C/EXEC SQL                                                         
    C+         FETCH C1 INTO                                           
    C+                  :NDTCFT,   :NDTCF2, :NDTCL2, :NDTCF4, :NDTCL4, 
    C+                  :NDSTPRCC, :NDPOD                              
    C/END-EXEC                                                         
    
    (process the received records here)
    C                   EndDo                             
                                                          
     * End of data - print the totals and end the report :
                                                          
    C     Finished      Tag                               
                                                          
    C/EXEC SQL                                            
    C+         CLOSE C1                                   
    C/END-EXEC                                            
    
    Regards, Sloopy
    2,195 pointsBadges:
    report
  • Alicsc
    Great !!! Thanks to all
    370 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following