Chain and Delete in Flat File in RPGLE

Chain and Delete in Flat File in RPGLE

Chain and Delete in Flat File in RPGLE, flat file, flat file in as400, delete records in flat file in RPGLE, as400, ibmi, iseries, systemi

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.

CRTPF, Create Flat File in AS400, Create File in IBMi
FLATFILE1

Insert data in a Flat file

Let's insert some records manually in a flat file named FLATFILE1.

Insert records in flat file, flat file in AS400,
Records in Flat File

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 DELETE in a Flat file.

Chain and Delete record in a flat-file in the RPGLE program

The below code will define and Delete 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 deleting the records in a flat-file in the RPGLE program.

RPG Code in Fixed format for Chain & Delete data in Flat file in AS400
*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                   DELETE    FLATFILE1@                           
C                   ENDIF                                          
C                   EVAL      *INLR = *ON
RPGLE Code in /Free and /End-Free format for Chain & Delete data in Flat file in AS400
*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; 
   DELETE FlatFile1@;
  ENDIF;                                     
  *Inlr = *On;                  
 /End-Free
RPGLE Code in Fully free format for Chain & Delete data in Flat file in AS400
**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;                                          
  DELETE 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 deleting 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 Update in Flat file in RPGLE

Post a Comment

© AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain