Creating a Logical File with More Than One Record Format

A multiple format logical file lets you use related records from two or more physical files by referring to only one logical file. Each record format is always associated with one or more physical files. You can use the same physical file in more than one record format.

Figure 10. DDS for a Physical File (ORDDTLP) Built from a Field Reference File

|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
     A* ORDER DETAIL FILE (ORDDTLP) - PHYSICAL FILE RECORD DEFINITION
     A                                      REF(DSTREF)
     A          R ORDDTL                    TEXT('Order detail record')
     A            CUST      R
     A            ORDER     R
     A            LINE      R
     A            ITEM      R
     A            QTYORD    R
     A            DESCRP    R
     A            PRICE     R
     A            EXTENS    R
     A            WHSLOC    R
     A            ORDATE    R
     A            CUTYPE    R
     A            STATE     R
     A            ACTMTH    R
     A            ACTYR     R
     A

Figure 11. DDS for a Physical File (ORDHDRP) Built from a Field Reference File

|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
     A* ORDER HEADER FILE (ORDHDRP) - PHYSICAL FILE RECORD DEFINITION
     A                                      REF(DSTREFP)
     A          R ORDHDR                    TEXT('Order header record')
     A            CUST      R
     A            ORDER     R
     A            ORDATE    R
     A            CUSORD    R
     A            SHPVIA    R
     A            ORDSTS    R
     A            OPRNME    R
     A            ORDMNT    R
     A            CUTYPE    R
     A            INVNBR    R
     A            PRTDAT    R
     A            SEQNBR    R
     A            OPNSTS    R
     A            LINES     R
     A            ACTMTH    R
     A            ACTYR     R
     A            STATE     R
     A

The following example shows how to create a logical file ORDFILL with two record formats. One record format is defined for order header records from the physical file ORDHDRP; the other is defined for order detail records from the physical file ORDDTLP. ( Figure 10 shows the DDS for the physical file ORDDTLP, Figure 11 shows the DDS for the physical file ORDHDRP, and Figure 12 shows the DDS for the logical file ORDFILL.)

The logical file record format ORDHDR uses one key field, Order, for sequencing; the logical file record format ORDDTL uses two keys fields, Order and Line, for sequencing.

Figure 12. DDS for the Logical File ORDFILL

|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
     A* ORDER TRANSACTION LOGICAL FILE (ORDFILL)
     A          R ORDHDR                    PFILE(ORDHDRP)
     A          K ORDER
     A
     A          R ORDDTL                    PFILE(ORDDTLP)
     A          K ORDER
     A          K LINE
     A

To create the logical file ORDFILL with two associated physical files, use a Create Logical File (CRTLF) command like the following:

CRTLF  FILE(DSTPRODLB/ORDFILL)
       TEXT('Order transaction logical file')

The DDS source is in the member ORDFILL in the file QDDSSRC. The file ORDFILL with a member of the same name is placed in the DSTPRODLB library. The access path for the logical file member ORDFILL arranges records from both the ORDHDRP and ORDDTLP files. Record formats for both physical files are keyed on Order as the common field. Because of the order in which they were specified in the logical file description, they are merged in Order sequence with duplicates between files retrieved first from the header file ORDHDRP and second from the detail file ORDDTLP. Because FIFO, LIFO, or FCFO are not specified, the order of retrieval of duplicate keys in the same file is not guaranteed.
Note:In certain circumstances, it is better to use multiple logical files, rather than to use a multiple-format logical file. For example, when keyed access is used with a multiple-format logical file, it is possible to experience poor performance if one of the files has very few records. Even though there are multiple formats, the logical file has only one index, with entries from each physical file. Depending on the kind of processing being done by the application program (for example, using RPG SETLL and READE with a key to process the small file), the system might have to search all index entries in order to find an entry from the small file. If the index has many entries, searching the index might take a long time, depending on the number of keys from each file and the sequence of keys in the index. (If the small file has no records, performance is not affected, because the system can take a fast path and avoid searching the index.)

Controlling How Records Are Retrieved in a File with Multiple Formats

In a logical file with more than one record format, key field definitions are required. Each record format has its own key definition, and the record format key fields can be defined to merge the records of the different formats. Each record format does not have to contain every key field in the key. Consider the following records:

Header Record Format:
Record Order Cust Ordate
1 41882 41394 050688
2 32133 28674 060288

Detail Record Format:
Record Order Line Item Qtyord Extens
A 32133 01 46412 25 125000
B 32133 03 12481 4 001000
C 41882 02 46412 10 050000
D 32133 02 14201 110 454500
E 41882 01 08265 40 008000

In DDS, the header record format is defined before the detail record format. If the access path uses the Order field as the first key field for both record formats and the Line field as the second key field for only the second record format, both in ascending sequence, the order of the records in the access path is:

Record 2
Record A
Record D
Record B
Record 1
Record E
Record C
Note:Records with duplicate key values are arranged first in the sequence in which the physical files are specified. Then, if duplicates still exist within a record format, the duplicate records are arranged in the order specified by the FIFO, LIFO, or FCFO keyword. For example, if the logical file specified the DDS keyword FIFO, then duplicate records within the format would be presented in first-in-first-out sequence.

For logical files with more than one record format, you can use the *NONE DDS function for key fields to separate records of one record format from records of other record formats in the same access path. Generally, records from all record formats are merged based on key values. However, if *NONE is specified in DDS for a key field, only the records with key fields that appear in all record formats before the *NONE are merged.

The logical file in the following example contains three record formats, each associated with a different physical file:
Record Format Physical File Key Fields
EMPMSTR EMPMSTR Empnbr (employee number) (1)
EMPHIST EMPHIST Empnbr, Empdat (employed date) (2)
EMPEDUC EMPEDUC Empnbr, Clsnbr (class number) (3)
Note:All record formats have one key field in common, the Empnbr field.

The DDS for this example is:



|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
      A
      A          K EMPNBR  (1)
      A
      A          K EMPNBR  (2)
      A          K EMPDAT
      A
      A          K EMPNBR  (3)
      A          K *NONE
      A          K CLSNBR
      A

*NONE is assumed for the second and third key fields for EMPMSTR and the third key field for EMPHIST because no key fields follow these key field positions.

The following shows the arrangement of the records:
  Empnbr   Empdat   Clsnbr Record Format Name
426

EMPMSTR
426 6/15/74
EMPHIST
426
412 EMPEDUC
426
520 EMPEDUC
427

EMPMSTR
427 9/30/75
EMPHIST
427
412 EMPEDUC

*NONE serves as a separator for the record formats EMPHIST and EMPEDUC. All the records for EMPHIST with the same Empnbr field are grouped together and sorted by the Empdat field. All the records for EMPEDUC with the same Empnbr field are grouped together and sorted by the Clsnbr field.
Note:Because additional key field values are placed in the key sequence access path to guarantee the above sequencing, duplicate key values are not predictable.

See the DDS Reference for additional examples of the *NONE DDS function.

Controlling How Records Are Added to a File with Multiple Formats

To add a record to a multiple format logical file, identify the member of the based-on physical file to which you want the record written. If the application you are using does not allow you to specify a particular member within a format, each of the formats in the logical file needs to be associated with a single physical file member. If one or more of the based-on physical files contains more than one member, you need to use the DTAMBRS parameter, described in Logical File Members, to associate a single member with each format. Finally, give each format in the multiple format logical file a unique name. If the multiple format logical file is defined in this way, then when you specify a format name on the add operation, you target a particular physical file member into which the record is added.

When you add records to a multiple-format logical file and your application program uses a file name instead of a record format name, you need to write a format selector program. For more information about format selector programs, see Identifying Which Record Format to Add in a File with Multiple Formats.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]