OPNQRYF is appropriate when you want complex dynamic selection outside of the application. You may have a standard report that runs for all salesmen or all customers. Someone asks for the report only for their customers or for a specific date range or both. Instead of creating a new report with the limited criteria, you create a CL with OPNQRYF to select the records to pass to the Report. The CL could have a command front end to let the user fill in the values for any number of fields, then build the selection parameter for the OPNQRYF.
This is an example CL to build the selection criteria:
PGM PARM(&INVDATE &SALE) /* Select Invoice Date +
DCL VAR(&INVDATE) TYPE(*CHAR) LEN(7)
DCL VAR(&IHIDAT) TYPE(*CHAR) LEN(8)
DCL VAR(&SALE) TYPE(*CHAR) LEN(3)
DCL VAR(&QRYSLT) TYPE(*CHAR) LEN(50)
CVTDAT DATE(&INVDATE) TOVAR(&IHIDAT) FROMFMT(*CYMD) +
TOFMT(*YYMD) TOSEP(*NONE) /* Convert date */
CHGVAR VAR(&QRYSLT) VALUE(‘IHIDAT *GE’ |> &IHIDAT +
|> ‘*AND IHSALE = ”’ |< &SALE |< ””)
OVRDBF FILE(SE1ISH) SHARE(*YES)
OPNQRYF FILE((SE1ISH)) OPTION(*ALL) QRYSLT(&QRYSLT) +
I had to create a selection function that required an index built over a compound key that was built from fields that came from two different files. OPNQRYF is the only way to generate such an index.