Using the Open Query File (OPNQRYF) command for more than just input

 

You can use the OPTION parameter of the Open Query File (OPNQRYF) command to specify the type of processing.

The default is OPTION(*INP); that is, the file is opened for input only. You can also use other OPTION values of the OPNQRYF command and a high-level language program to add, update, or delete records through the open query file.

However, if you specify the UNIQUEKEY, GRPFLD, or GRPSLT parameter, use one of the aggregate functions, or specify multiple files on the FILE parameter, your use of the file is restricted to input only.

A join logical file is limited to input-only processing. A view is limited to input-only processing, if group, join, union, distinct processing, or a user-defined table function is specified in the definition of the view. If the query optimizer needs to create a temporary file to implement the query, then the use of the file is restricted to input only. If you want to change a field value from the current value to a different value in some of the records in a file, you can use a combination of the OPNQRYF command and a specific high-level language program. For example, assume that you want to change all the records where the Flda field is equal to ABC so that the Flda field is equal to XYZ. You can specify:

OVRDBF    FILE(FILEA) SHARE(*YES)
OPNQRYF   FILE(FILEA) OPTION(*ALL) QRYSLT('FLDA *EQ "ABC" ')
CALL      PGM(PGMA)
CLOF      OPNID(FILEA)
DLTOVR    FILE(FILEA)

Program PGMA processes all records it can read, but the query selection restricts these to records where the Flda field is equal to ABC. The program changes the field value in each record to XYZ and updates the record. You can also delete records in a database file using the OPNQRYF command. For example, assume that you have a field in your record that, if equal to X, means the record should be deleted. Your program can be written to delete any records it reads and use the OPNQRYF command to select those to be deleted such as:

OVRDBF    FILE(FILEA) SHARE(*YES)
OPNQRYF   FILE(FILEA) OPTION(*ALL) QRYSLT('DLTCOD *EQ "X" ')
CALL      PGM(PGMB)
CLOF      OPNID(FILEA)
DLTOVR    FILE(FILEA)

You can also add records by using the OPNQRYF command. However, if the query specifications include selection values, your program can be prevented from reading the added records because of the selection values.

 

Parent topic:

Using Open Query File (OPNQRYF) command