Chain and Update in Flat file in RPGLE |
Introduction
A flat file is a file without DDS but with record length. A flat file has the same name record format and field name with a field size as the record length.
Create command
A flat file can be created using the CRTPF command as follows. Let's create a flat file named FLATFILE1 in the current library.
FLATFILE1 |
Insert data in a Flat file
Let's insert some records manually in a flat file named FLATFILE1.
We can perform the READ, WRITE, UPDATE, DELETE operation on the flat file using I/O in RPGLE. So In this blog, we will discuss the example program to do CHAIN and UPDATE in Flat file.
Chain and Update record in a flat-file in the RPGLE program
The below code will define and update data using chain in a flat-file into a variable defined in RPGLE program. You can create the program either by taking option 14 of the source member on PDM or using the CRTSQLRPGI command on SQLRPGLE source member.
Let me explain the code which is written above for chaining and updating the records in a flat-file in the RPGLE program.
*Header Specification
HDebug(*Yes)
HOption(*NoDebugio)
FFlatFile1 UF E DISK RENAME(FLATFILE1:FLATFILE1@)
F PREFIX(t)
* program variables
D LocalVariable S 100A INZ('CHANGE DATA')
C 2 CHAIN FLATFILE1
C IF %FOUND(FLATFILE1)
C EVAL tFlatFile1 = LocalVariable
C UPDATE FLATFILE1@
C ENDIF
C EVAL *INLR = *ON
*Header Specification
HDebug(*Yes)
HOption(*NoDebugio)
FFlatFile1 UF E DISK RENAME(FLATFILE1:FLATFILE1@)
F PREFIX(t)
* program variables
*
D LocalVariable S 100A INZ('CHANGE DATA')
/Free
CHAIN 2 FlatFile1;
IF %found(flatfile1);
tflatfile1 = LocalVariable;
UPDATE FlatFile1@;
ENDIF;
*Inlr = *On;
/End-Free
**FREE
DCL-F FLATFILE1 USAGE(*UPDATE) PREFIX(T) RENAME(FLATFILE1:FLATFILE1@);
DCL-S LOCALVARIABLE CHAR(100) INZ('CHANGE DATA');
CHAIN 2 FlatFile1;
IF %found(flatfile1);
tflatfile1 = LocalVariable;
UPDATE FlatFile1@;
ENDIF;
*Inlr = *On;
The code defines a flat file named FLATFILE1 in RPG in Update fully procedural mode and it's an external disk file. RENAME is used to rename the record format of the file from FLATFILE1 to FLATFILE1@ and prefix the field with t so that field becomes tFLATFILE1.
Please note that we need to RENAME the record format to ignore compile-time severity 40 error *RNF2109 (All record formats for externally-described files ignored or dropped due to error.
Also, we need to PREFIX the field, otherwise, we will again get compile-time severity 30 error *RNF7503 (Expression contains an operand that is not defined)
Declare a variable of length 100 since the field length of a flat file is 100.
The chain on flat file using RRN 2 and if record foud then evaluates the prefixed flat-file field tFLATFILE to LocalVariable in RPG program and then updating the Record format FLATFILE1@. Finally setting the last record indicator to *ON.
Related Post
Flat File in AS400
Reading Flat File in RPGLE
Writing in flat file in RPGLE
Chain and Delete in Flat File in RPGLE