• No results found

Creating a logical file with more than one record format

You can create a logical file using data description specifications (DDS).

The physical file or files on which the logical file is based must already exist before you create a logical file.

To create a logical file, follow these steps:

1. Type the DDS for the logical file into a source file.

You can do this using the source entry utility (SEU). The following example shows the DDS for logical file ORDHDRL (an order header file):

|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 A* ORDER HEADER LOGICAL FILE (ORDHDRL)

A R ORDHDR PFILE(ORDHDRP) A K ORDER

This file uses the key field Order (order number) to define the access path. The record format is the same as the associated physical file ORDHDRP. The record format name for the logical file must be the same as the record format name for the physical file because no field descriptions are given.

2. Create the logical file.

You can use the Create Logical File (CRTLF) command. The following example shows how the CRTLF command can be typed:

CRTLF FILE(DSTPRODLB/ORDHDRL)

TEXT('Order header logical file')

As shown, this command uses some defaults. For example, because the SRCFILE and SRCMBR

parameters are not specified, the system uses DDS from the IBM-supplied source file QDDSSRC, and the source file member name is ORDHDRL (the same as the file name specified on the CRTLF command). The ORDHDRL file with one member of the same name is placed in the DSTPRODLB library.

You can create multiple logical files over a single physical file. The maximum number of logical files that can be created over a single physical file is 32KB.

Views are similar to logical files. You can create views using System i Navigator or using the CREATE VIEW SQL statement.

Related concepts Working with source files

You can use various methods to enter and maintain data in a source file.

Identifying which record format to add in a file with multiple formats

If your application uses a file name instead of a record format name for records to be added to the database, and if the file used is a logical file with more than one record format, you need to write a format selector program to determine where a record should be placed in the database.

Creating and using a view Related reference

Create Logical File (CRTLF) command CREATE VIEW

Creating a logical file with more than one record format

A multiple-format logical file allows you to use records from two or more physical files by referring to only one logical file.

Each record format of such a multiple-format logical file is always associated with one or more physical files. You can use the same physical file in more than one record format.

The following example shows the data description specifications (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)

The following example shows the DDS for the ORDHDRP physical file:

|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 A* ORDER HEADER FILE (ORDHDRP) - PHYSICAL FILE RECORD DEFINITION

A REF(DSTREFP)

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 ORDHDRP physical file; the other is defined for order detail records from the ORDDTLP physical file.

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.

The following example shows the DDS for the ORDFILL logical file:

|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 A* ORDER TRANSACTION LOGICAL FILE (ORDFILL)

A R ORDHDR PFILE(ORDHDRP)

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

CRTLF FILE(DSTPRODLB/ORDFILL)

TEXT('Order transaction logical file')

The DDS source is in the ORDFILL member of the QDDSSRC file. The ORDFILL file with a member of the same name is placed in the DSTPRODLB library. The access path for the ORDFILL logical file member 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 ORDHDRP header file and second from the ORDDTLP detail file. Because FIFO, LIFO, or FCFO are not specified, the order of retrieval of duplicate keys in the same file is not guaranteed.

Note: In some 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 logical file with multiple formats

Key field definitions are required in a logical file with more than one record format.

Each record format can have its own key field definition. The record format key fields can be defined to merge the records from different formats. Each record format does not have to contain every key field in the key.

Consider the following records.

Table 3. Header record format

Record Order Cust Ordate

1 41882 41394 050688

2 32133 28674 060288

Table 4. 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 data description specifications (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. When such records are retrieved by key from more than one record format, only key fields that appear in all record formats before the *NONE are used. To increase the number of key fields that are used, limit the number of record formats considered.

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 table 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.

Related concepts DDS concepts

Controlling how records are added to a logical file with multiple formats

To add a record to a multiple-format logical file, you need to identify the member of the based-on physical file to which you want the record to be 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 contain more than one member, you need to use the DTAMBRS parameter, described in “Defining logical file members” on page 41, 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.

Related concepts

Identifying which record format to add in a file with multiple formats

If your application uses a file name instead of a record format name for records to be added to the database, and if the file used is a logical file with more than one record format, you need to write a format selector program to determine where a record should be placed in the database.