Example 1: Define fields derived from existing field definitions

This example shows the use of derived fields.

Assume that you have the Price and Qty fields in the record format. You can multiply one field by the other by using the Open Query File (OPNQRYF) command to create the derived Exten field. You want FILEA to be processed, and you have already created FILEAA. Assume that the record formats for the files contain the following fields:

FILEA FILEAA
Order Order
Item Item
Qty Exten
Price Brfdsc
Descrp  

The Exten field is a mapped field. Its value is determined by multiplying Qty times Price. It is not necessary to have either the Qty or Price field in the new format, but they can exist in that format, too, if you want. The Brfdsc field is a brief description of the Descrp field (it uses the first 10 characters).

Assume that you have specified PGMF to process the new format. To create this program, use FILEAA as the file to read. You can specify:
OVRDBF     FILE(FILEAA) TOFILE(FILEA) SHARE(*YES)
OPNQRYF    FILE(FILEA) FORMAT(FILEAA) +
              MAPFLD((EXTEN 'PRICE * QTY') +
              (BRFDSC 'DESCRP'))
CALL       PGM(PGMF) /* Created using file FILEAA as input */
CLOF       OPNID(FILEA)
DLTOVR     FILE(FILEAA)

Notice that the attributes of the Exten field are those defined in the record format for FILEAA. If the value calculated for the field is too large, an exception is sent to the program.

It is not necessary to use the substring function to map to the Brfdsc field if you only want the characters from the beginning of the field. The length of the Brfdsc field is defined in the FILEAA record format.

All fields in the format specified on the FORMAT parameter must be described on the OPNQRYF command. That is, all fields in the output format must either exist in one of the record formats for the files specified on the FILE parameter or be defined on the MAPFLD parameter. If you have fields in the format on the FORMAT parameter that your program does not use, you can use the MAPFLD parameter to place zeros or blanks in the fields. Assume the Fldc field is a character field and the Fldn field is a numeric field in the output format, and you are using neither value in your program. You can avoid an error on the OPNQRYF command by specifying:
MAPFLD((FLDC ' " " ')(FLDN 0))

Notice quotation marks enclose a blank value. By using a constant for the definition of an unused field, you avoid having to create a unique format for each use of the OPNQRYF command.