chain

765 Views


The CHAIN operation retrieves a record from a file and places the data from the record into the input fields. 

The CHAIN operation applies a record lock to files that are open in update mode. 

The search argument, search-arg, must be the key or relative record number used to retrieve the record. 

It can be a KLIST name, a list of values, or %KDS.

%KDS(data-structure-name{:num-keys})

Syntax:

CHAIN{(ENHMR)} search-arg name {data-structure}
Chain %Kds( Key_DS : Number_of_keys )                
      filename
      ds_fileName_IO ;                                       
                                                            
If %found(filename);                                        
                                                            
   Delete(e) filename;                                  
                                                            
   If %error;                                               
      Success = *Off ;                               
   Else;                                                    
      Success = *On;                                
   EndIf;                                                   
                                                            
EndIf;                                                      
Chain ( parm_DS.Nbr                                                  
      : parm_DS.Seq)               // Keys                  
      filename                     // File                  
      ds_filename_IO;              // Input DS              
                                                                                        
If %found(filename);
   parm_filename_DS = ds_filename_IO;                                                                    
   Success = *On;                                                               
Endif;                                                                                  

 

 chain(e) %kds( i: 2 ) @upd;  
                              
                              
 // if found, update it.      
 if %found and not %error;    
    if i <> o;                
       o = i;                 
       if pMode = $update;    
          update(e) @upd;     
       else;                  
          unlock(e) filename; 
       endif;                 
       if %error;             
          stats.upderr += 1;  
       else;                  
          stats.updok += 1;   
       endif;                 
    else;                     
       stats.noupdate += 1;   
      unlock(e) filename;  
  endif;   
01  dcl-f TESTFILE keyed ;

02  dcl-s wkFld1 like(FLD001) ;
03  dcl-s wkFld2 like(FLD002) ;
04  dcl-s wkFld3 like(FLD003) ;

05  wkFld1 = '3' ;
06  wkFld2 = '3' ;
07  wkFld3 = '3' ;

08  chain (wkFld1:wkFld2:wkFld3) TESTFILER ;

09 chain ('3':'3':'3') TESTFILER ;
A..........T.Name++++++RLen++TDpB......Functions++++++++++++++++++
A          R CUSTR
A            NAME         100A
A            ZIP           10A
A            ADDR         100A
A          K NAME
A          K ZIP
FFilename++IPEASF.....L.....A.Device+.Keywords+++++++++++++++++++++++++
Fcustfile  if   e           k disk    rename(CUSTR:custRec)
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++
D custRecKeys     ds                  likerec(custRec : *key)
D numKeys          s            10i 0
 ...
 /free
         // custRecKeys is a qualified data structure
         custRecKeys.name = customer;
         custRecKeys.zip = zipcode;
         // The *KEY data structure is used as the search argument for CHAIN
         chain %kds(custRecKeys) custRec;
         // The number of keys can be a constant
         chain %kds(custRecKeys : 2) custRec;
         // The number of keys can be a variable or an expression
         numKeys = 1;
         chain %kds(custRecKeys : numKeys) custRec;
         chain %kds(custRecKeys : numKeys + 1) custRec;
 /end-free
01  dcl-f TESTFILE keyed ;

02  dcl-ds KeyTestfile likerec(TESTFILER:*key) ;

03  KeyTestfile.FLD001 = '3' ;
04  KeyTestfile.FLD002 = '3' ;
05  KeyTestfile.FLD003 = '3' ;

06  chain %kds(KeyTestfile) TESTFILER ;
CHAIN(N)

The CHAIN operation applies a record lock to files that are open in update mode. To avoid lock we use operator extender 'N'.

Other Links on Chain:

IBM: Chain

Chain More Info

Go4as400: https://www.go4as400.com/rpgle-opcodes/as400.aspx?cid=211

AS400 Tricks: Chain

RPGPGM: Chain %Kds

Post Comments