Friday, July 4, 2008

Flat File

Flat File:
Flat files are physical files that have no field definitions and no indexes built off of them. Flat files, Record format and Field are all same. They all have a same name.
We can write, read, update, delete the Flat file.

Reading and Deleting can be done normally where as update and writing requires the use of Data Structures. Let me discuss one by one.

Creating a Flat File:

Creating a flat file is same as creating Physical File. It is very easy to declare either in command line or even in DDS in STRSEU editor.

CRTPF(Flatfilename) RCDLEN(length)

Consider an example:

CRTPF(Flatfilename) RCDLEN(length)


Writing a Flat File:
Writing can be done either in RPG-400 or In RPG ILE also.


F flatfile O F 150 DISK

1. In F spec File is declared, with Output mode [ O ] and File is described as Program described File [ F ]. And record length is also [ 150 ]declared.
2. We are selecting IPDS prompt to enter the Data Structure with the size of the flat file.( Actually 52 is the maximum length to display data structure in RPG-400).

IDS01 DS 150

3. Then we have to select IPJ prompt and specify the length of the subfields and name them.
We can either make use of them as Packed Decimals, Character and zoned decimals.
I P 1 50CUNO
I 6 24 CUNA
I 25 25 AMOL
I 26 350BALA
I 36 36 DEPL
I 37 470DEPO
I 48 48 WITL
I 49 590WITH

4. After giving the value then assign to them according to the Character and numerical (data type).

C Z-ADD10003 CUNO
C MOVEL'BEMA' CUNA
C MOVE 'A' AMOL
C MOVE 'D' DEPL
C MOVE 'W' WITL
C Z-ADD660 DEPO
C Z-ADD200 WITH
C DEPO SUB WITH BALA
C Z-ADDBALA BALA
C WRITE flatfile DS01

5. While you are writing the flat file you have to specify even the Data structure name in Result field.
C SETON LR

CHAIN in Flat File:

Flat file can be chained by using RRN value (Relative Record Number). According to that we can CHAIN particular record where we have to make changes. Or Delete particular Record.


Updating a Flat File:
It is also same as the WRITE operation. After specifying particular changes just use UPDATE FLATFILE with DATASTRUCTURE.


Deleting a Flat File:
It also same as UPDATE. After getting the RRN of record by CHAIN Operation, then we can DELETE using Delete operation.

C DELETE flat file name


Reading a Flat File:

Reading a Flat file is slightly different. Here we can use IPI prompt. Let me explain one by one.


FGFLAT IF F 150 DISK

After F-(file declaration) We can Take IPI prompt then we may get prompt like below picture.


A).We have to declare the file name. In Sequence we can give any two alphabets, (if specified then it will be in special sequenced mode), else if we specify any two numbers then it will be in non-sequenced mode). (While retrieving the data’s from the flat file).
B). If we specify the RECORD ID then will taken as indicator.(01 in e.g)
C) In Record Identification Codes we can specify the position , whether the searched one should have particular character at (this position) . I can be a character or zoned number or a digit. If so What character in character space.

2. After giving the values it will be as below line.

IGFLAT AA 01 6 CC

3. Then we have to take IPJ prompt to enter the details of position , fields , packed filed, character, zoned one. In RPGLE we can also have date fields, time fields (that too including the particular which format we need)

I P 1 50CUNO
I 6 24 CUNA
I 25 25 AMOL
I 26 350AMOU
I AA 02 6 CH
I 6 24 HUNA
I 36 36 DEPL
I 37 470DEPO
I 48 48 WITL
I 49 590WITH
I AA 03
I P 1 50MENO
I 6 24 MENA
I 25 25 MEOL
I 26 350MEOU


4. When a read operation is encountered, then control will get transferred to I spec. According to the condition particular indicator will get switched on. It will take particular records. Using that control will get transferred below the READ statement.

C READ GFLAT 25
C *IN25 DOWEQ*OFF
C *IN01 IFEQ *ON
C CUNO DSPLY
C CUNA DSPLY
C AMOL DSPLY
C AMOU DSPLY
C SETOF 01
C ENDIF
C *IN02 IFEQ *ON
C HUNA DSPLY
C DEPO DSPLY
C WITH DSPLY
C SETOF 02
C ENDIF
C *IN03 IFEQ *ON
C MENO DSPLY
C MENA DSPLY
C MEOL DSPLY
C MEOU DSPLY
C SETOF 03
C ENDIF
C READ GFLAT 25
C ENDDO
C SETON LR


ADVANTAGE OF THIS KIND OF READ :

When I use I SPEC with INDICATORS, then program can make use of any group of fields according to the INDICATOR selection.
So in a single file we can access RECORDS in what ever condition we specified.

OVERIDING :

Overriding a flat file is also possible. We can generate multiple members. Rule for members are also same as the flat file. (Same name for members and fields and record formats).

No comments: