This example shows how to dynamically join database files without DDS.
Assume that you want to join FILEA and FILEB, and the files contain the following fields:
|
The join field is Cust which exists in both files. Any record format name can be specified on the Open Query File (OPNQRYF) command for the join file. The file does not need a member. The records are not required to be in keyed sequence.
OVRDBF FILE(JOINAB) TOFILE(FILEA) SHARE(*YES) OPNQRYF FILE(FILEA FILEB) FORMAT(JOINAB) + JFLD((FILEA/CUST FILEB/CUST)) + MAPFLD((CUST 'FILEA/CUST')) CALL PGM(PGME) /* Created using file JOINAB as input */ CLOF OPNID(FILEA) DLTOVR FILE(JOINAB)
File JOINAB is a physical file with no data. This file contains the record format to be specified on the FORMAT parameter of the OPNQRYF command.
Notice that the TOFILE parameter on the Override with Database File (OVRDBF) command specifies the name of the primary file for the join operation (the first file specified for the FILE parameter on the OPNQRYF command). In this example, the FILE parameter on the OPNQRYF command identifies the files in the sequence they are to be joined (A to B). The format for the file is in the file JOINAB.
OPNQRYF FILE(FILEA FILEB) FORMAT(JOINAB) + QRYSLT('FILEA/CUST *EQ FILEB/CUST') + MAPFLD((CUST 'FILEA/CUST'))
The system joins FILEA and FILEB using the Cust field because of the values specified for the QRYSLT parameter. Notice that in this example the JFLD parameter is not specified on the command. However, if either JDFTVAL(*ONLYDFT) or JDFTVAL(*YES) is specified on the OPNQRYF command, the JFLD parameter must be specified.
JFLD((CUST FILEB/CUST)) + MAPFLD((CUST 'FILEA/CUST'))
Any other uses of the same field name in the OPNQRYF command to indicate a field from a file other than the file defined by the MAPFLD parameter must be qualified with a file name.
Because no KEYFLD parameter is specified, the records appear in any sequence depending on how the OPNQRYF command selects the records. You can force the system to arrange the records the same as the primary file. To do this, specify *FILE on the KEYFLD parameter. You can specify this even if the primary file is in arrival sequence.
The JDFTVAL parameter (similar to the JDFTVAL keyword in DDS) can also be specified on the OPNQRYF command to describe what the system should do if one of the records is missing from the secondary file. In this example, the JDFTVAL parameter was not specified, so only the records that exist in both files are selected.
If you tell the system to improve the results of the query (through parameters on the OPNQRYF command), it generally tries to use the file with the smallest number of records selected as the primary file. However, the system also tries to avoid building a temporary file.
You can force the system to follow the file sequence of the join as you have specified it in the FILE parameter on the OPNQRYF command by specifying JORDER(*FILE). If JDFTVAL(*YES) or JDFTVAL(*ONLYDFT) is specified, the system will never change the join file sequence because a different sequence can cause different results.