HP RPG/XL Programmer's Guide (30318-90001)

3- 5
Reading a KSAM Disc File
There are several ways to read records in a KSAM file. Three methods,
sequential, random and chronological, are discussed in this chapter.
Reading a file sequentially means that RPG retrieves records
automatically in key sequence. You can process the entire file
automatically or you can process a portion of the file. To process part
of the file, you give RPG the key value of the first record to access.
Records are retrieved starting with that record and proceeding
sequentially until the last record (having a certain key value or
end-of-file) is processed.
Reading a file randomly means that you supply RPG with the key values for
each record to be processed. RPG goes to those records directly, without
passing through others first.
Reading a KSAM file chronologically means retrieving records in the
sequence in which they were added to the file.
Reading a KSAM File Sequentially. This section explains how to read
records in a KSAM file sequentially, starting with the first record in
the file and ending with the last. You can access records in sequence by
any key field. You can also read records in sequence by a non-key field.
To do this, the KSAM file must first be sorted on that field.
The following lines list the KSAMUTIL commands that create the KSAM file
used in Figure 3-3 and Figure 3-4. The KSAM file, MASTFL, has two keys.
The primary key field (KEY=B,1,4) does not have duplicates. The
secondary key field (KEY=B,25,15,,RDUP) can have duplicates but they are
not maintained in chronological order.
:RUN KSAMUTIL.PUB.SYS
>BUILD MASTFL;REC=-256,4,F,ASCII;DISC=20000,20,4;KEYFILE=MASTFLK;&
>KEY=B,1,4;KEY=B,25,15,,RDUP
Reading a KSAM File Sequentially by Key
This section explains how to read an entire KSAM file sequentially by
key. You can read a KSAM file in order by any field that was specified
as a key when the file was created.
Figure 3-3 shows the File Description Specification that reads the KSAM
file MASTFL by its primary key. (The previous section shows how MASTFL
is created.) MASTFL is defined as a primary file. If the KSAM file is
defined as a demand file, include a Calculation Specification containing
the READE operation.
Figure 3-3. Reading a KSAM File Sequentially by Key
Comments
1 This line defines the KSAM file, MASTFL.
Columns 29-30 contain 4 to specify the length of the key, DEPT.