Example 3: Define fields derived from existing field definitions

This example shows the use of derived fields and built-in functions.

Assume, in the previous example, that a field called Fldx also exists in FILEA, and the Fldx field has appropriate attributes used to hold the sine of the Fldm field. Also assume that you are not using the contents of the Fldx field. You can use the MAPFLD parameter to change the contents of a field before passing it to your high-level language program. For example, you can specify:
OVRDBF     FILE(FILEA) SHARE(*YES)
OPNQRYF    FILE(FILEA) MAPFLD((FLDX '%SIN(FLDM)'))
CALL       PGM(PGMF) /* Created using file FILEA as input */
CLOF       OPNID(FILEA)
DLTOVR     FILE(FILEA)

In this case, you do not need to specify a different record format on the FORMAT parameter. (The default uses the format of the first file on the FILE parameter.) Therefore, the program is created by using FILEA. When using this technique, you must ensure that the field you redefine has attributes that allow the calculated value to process correctly. The least complicated approach is to create a separate file with the specific fields you want to process for each query.

You can also use this technique with a mapped field definition and the %XLATE function to translate a field so that it appears to the program in a different manner than what exists in the database. For example, you can translate a lowercase field so the program only sees uppercase.

The sort sequence and language identifier can affect the results of the %MIN and %MAX built-in functions. For example, the uppercase and lowercase versions of letters can be equal or unequal depending on the selected sort sequence and language identifier.
Note: The translated field value is used to determine the minimum and maximum, but the untranslated value is returned in the result record.

The example described uses FILEA as an input file. You can also update data using the Open Query File (OPNQRYF) command. However, if you use a mapped field definition to change a field, updates to the field are ignored.