as400 interview questions and answers-Part 20
1. Explain chain opcode in RPGLE with example.
Ans:
Chain
·
The CHAIN command does a SETLL and
a READE in order to find a match. CHAIN is best used to locate a unique record
(like a customer record) from a full procedural file.
·
In case of CHAIN, the file
operation is used to randomly retrieve a record from a file.
·
We cannot move to another record, which meets
the same criteria specified in the Chain operation. So the conclusion is we
cannot read more number of records with the Chain operation, if there are more
number of records exists with the same criteria in the file.
·
The CHAIN operation applies a
record lock to files that are open in update mode. To avoid lock we use
operator extender ‘N’.
·
The search argument must be the
key or key list or relative record number.
·
If the corresponding record for
the search argument is not found then the HI level indicator gets turned on and
if found gets turned OFF. %FOUND built-in function also does the same thing by
returning '0' if no record is found and ‘1’ if a record is found.
·
To handle CHAIN exceptions (file
status codes greater than 1000), either the operation code extender 'E' or an
error indicator at LO level can be used.
·
If the data-structure is specified in
result field then the result of the operation is filled in the data structure.
Factor 1
|
Op-code
|
Factor 2
|
Result Field
|
Resulting Indicators
HI LO EQ
|
Search argument
|
CHAIN(E N)
|
File or Record format name
|
Data structure
|
No record found
|
Error
|
|
Example
I.
Using field
value in factor-1 with CHAIN operation
0171.00 C EDTSR BEGSR
0172.00 C EVAL K_PNUM=S_PNUM
0173.00 C K_PNUM CHAIN(E) REC2
0174.00 C IF %FOUND()
0175.00 C EVAL PADDR=S_PADDR
0176.00 C UPDATE REC2
0177.00 C ENDIF
0178.00 C ENDSR
II.
Using RRN value
in factor-1 with CHAIN operation
0130.00 C UPDSFL BEGSR
0131.00 C EVAL RRN=1
0132.00 C DO SFLPAG
0133.00 C RRN CHAIN EXPD_SFL
0134.00 C UPDATE EXPD_SFL
0135.00 C EVAL RRN=RRN+1
0136.00 C ENDDO
0137.00 C ENDSR
III.
Using keylist in
factor-1 with CHAIN operation in FREE format RPG
* Key list
C Klst1 KLIST
C Kfld FILED01
C Kfld FILED02
/FREE
F03RRN=0;
// Intitalise klst1
Chain klst1 FILE01;
If %found(FILE01);
C#PDES=PDES;
Endif;
/END-FREE
IV.
Using multiple
key field(including constant) in factor-1 with CHAIN operation in FREE format
RPG
/FREE
Chain (CO:DSREF:'3') FILE01;
If %found(FILE01);
Chain (FILED01:%subst(SREF01:1:7): %int(%subst(SREF01:8:3))) FILE02;
If %found(FILE02);
If QTAL02<>0;
--------------
--------------
--------------
Endif
Endif;arun
Endif;
/END-FREE
V.
Using data
structure in Result-field with CHAIN operation
Columns . . . : 6 80 Browse AMINEM/QRPGLESRC
SEU==> OP_CHAINDS
FMT FX FFilename++IPEASF.....L.....A.Device+.Keywords+++++++++++++++++++++++++++++
*************** Beginning of data ***************************************************
0001.00 Fcust IF E DISK 130128
0002.00 DDS1 DS LIKEREC(CUSTR) 130128
0003.00 C 5 CHAIN cust DS1 130128
0004.00 C IF %FOUND(cust) 130128
0005.00 C DS1 DSPLY 130128
0006.00 C ELSE 130128
0007.00 C 'NOT FOUND' DSPLY 130128
0008.00 C ENDIF 130128
0009.00 C SETON LR 130128
****************** End of data *******************************************************
Output
EVAL DS1
DS1.CSNBR = '000005'
DS1.CSNAME = 'HARISH '
DS1.CS#OPN = 00980.
DS1.CS$OPN = 00798789.00