Example 1: Select records using the Open Query File (OPNQRYF) command

This example shows how to select records with a specific value using the Open Query File (OPNQRYF) command.

Assume that you want to select all the records from FILEA where the value of the Code field is D. Your processing program is PGMB. PGMB only sees the records that meet the selection value (you do not have to test in your program).
Note: You can specify parameters easier by using the prompt function for the OPNQRYF command. For example, you can specify an expression for the QRYSLT parameter without the surrounding delimiters because the system will add the single quotation marks.
Specify the following parameters:
OVRDBF     FILE(FILEA) SHARE(*YES)
OPNQRYF    FILE(FILEA) QRYSLT('CODE *EQ "D" ')
CALL       PGM(PGMB)
CLOF       OPNID(FILEA)
DLTOVR     FILE(FILEA)
Notes:
  1. The entire expression in the QRYSLT parameter must be enclosed in single quotation marks (' ').
  2. When specifying field names in the OPNQRYF command, the names in the record are not enclosed in quotation marks.
  3. Character literals must be enclosed by quotation marks (" "). (The quotation mark character is used in the examples.) It is important to place the character(s) between the quotation marks in either uppercase or lowercase to match the value you want to find in the database. (The examples are all shown in uppercase.)
  4. To request a selection against a numeric constant, specify:
    OPNQRYF    FILE(FILEA) QRYSLT('AMT *GT 1000.00')
    Note:

    Numeric constants are not enclosed by quotation marks.

  5. When comparing a field value to a control language (CL) variable, use apostrophes as follows (only character CL variables can be used):
    • If doing selection against a character, date, time, or timestamp field, specify:
      OPNQRYF    FILE(FILEA) QRYSLT('"' *CAT &CHAR *CAT '" *EQ FIELDA')
      or, in reverse order:
      OPNQRYF    FILE(FILEA) QRYSLT('FIELDA *EQ "' *CAT &CHAR *CAT '"')
      Note:
      Single quotation marks (' ') and quotation marks (" ") enclose the CL variables and *CAT operators.
    • If doing selection against a numeric field, specify:
      OPNQRYF    FILE(FILEA) QRYSLT(&CHARNUM *CAT ' *EQ NUM')
      or, in reverse order:
      OPNQRYF    FILE(FILEA) QRYSLT('NUM *EQ ' *CAT &CHARNUM);
      Note:
      Single quotation marks enclose the field and operator only.

When comparing two fields or constants, the data types must be compatible. The following table describes the valid comparisons.

Table 1. Valid data type comparisons for the OPNQRYF command
  Any numeric Character Date1 Time1 Timestamp1
Any numeric Valid Not valid Not valid Not valid Not valid
Character Not valid Valid Valid2 Valid2 Valid2
Date1 Not valid Valid2 Valid Not valid Not valid
Time1 Not valid Valid2 Not valid Valid Not valid
Timestamp1 Not valid Valid2 Not valid Not valid Valid

1 Date, time, and timestamp data types can be represented by fields and expressions, but not constants; however, character constants can represent date, time, or timestamp values.

2 The character field or constant must represent a valid date value if compared to a date data type, a valid time value if compared to a time data type, or a valid timestamp value if compared to a timestamp data type.

The performance of record selection can be greatly enhanced if a file on the system uses the field being selected in a keyed sequence access path. This allows the system to quickly access only the records that meet the selection values. If no such access path exists, the system must read every record to determine if it meets the selection values.

Even if an access path exists on the field you want to select from, the system might not use the access path. For example, if it is faster for the system to process the data in arrival sequence, it will do so.

Related concepts
Double-byte character set considerations
Open Query File (OPNQRYF) command: Performance considerations