OPNQRYF QRYSLT Templates
As an OPNQRYF beginner, I stumbled along making the usual mistakes as I tried to create the Query Select (QRYSLT) expression. I often received error message CPD3129 ("Missing operand on expression...") and frequently got lost in quotes and *BCATs. However, I have since discovered a technique that can make building QRYSLT expressions easier.
1. I declare a &QRYSLT variable, giving it an initial value that exactly matches what I'll need in the QRYSLT parameter of the OPNQRYF command. In this initial value, I use X's to substitute any character values and 9's to substitute any numeric values.
2. Then I use the Substring (%SST) function in a Change Variable (CHGVAR) command to replace the X's and 9's with the actual values I require in my QRYSLT expression. In the example illustrated below, the user is expected to give &STATE a two-character value and &ZIP a five-digit value which the program receives as parameters.
PGM PARM(&STATE &ZIP) DCL VAR(&STATE) TYPE(*CHAR) LEN(2) DCL VAR(&ZIP) TYPE(*DEC) LEN(5 0) DCL VAR(&QRYSLT) TYPE(*CHAR) LEN(512) + /* ....+... 1 ...+... 2 ...+... 3 . */ + VALUE('STATE *EQ "XX" *OR ZIP *EQ 99999') CHGVAR VAR(%SST(&QRYSLT 12 2)) VALUE(&STATE) CHGVAR VAR(%SST(&QRYSLT 28 5)) VALUE(&ZIP) OPNQRYF FILE((CUSTOMER)) QRYSLT(&QRYSLT) ENDPGM
When the CHGVAR commands are executed, the computer "sees" the &QRYSLT variable as shown below.
/* ....+... 1 ...+... 2 ...+... 3 . */ QRYSLT('STATE *EQ "CA" *OR ZIP *EQ 92008')
Notice the placement of the comment containing a scale. This eliminates the tedious counting of characters to determine where, along &QRYSLT, to substitute the actual values. This method also eliminates the need to use concatenation operators and significantly reduces the number and placement of quotes. The important thing to remember is that if the variable part of the string is alphanumeric, enclose it in double quotes. It's easier to use one double quote character (") than to use the single quote (') twice. If you do use the single quote characters, the start position of the substitution variable will actually be one less than indicated for every pair of quotes encountered reading the template statement from left to right. In my example, I kept things simple and used the one double quote around my character literal.
OPNQRYF and MDY Dates
You can use the Range (%RANGE) function to perform date selection in the OPNQRYF command, even when the dates are in *MDY format and packed. Just use the Mapped Field (MAPFLD) parameter. For example, the following command selects only records whose LRDAT field (which is a packed date in *MDY format) falls within 1992:
OPNQRYF FILE(LRDS) QRYSLT('yymmdd = + %RANGE(''920101'' ''921231'')') + MAPFLD((DATE1 LRDAT *ZONED 6 0) + (DATE2 DATE1 *CHAR 6) (YYMMDD + '(%SST(DATE2 5 2) *CAT + %SST(DATE2 1 4))' *CHAR 6))
Note that if the date is already in zoned or character format, the first or first and second mapped-field specifications can be eliminated respectively. This still allows updates and deletes of the data.
You can sort by date this way by adding a mapped field specification to map the new date back to another field in the file that you specify on the Key Field (KEYFLD) parameter. The field must be one that you don't need for that application. Data will not be affected; however, you cannot update the file when this map is added.
QRYSLT with Binary Fields
When you need to preselect records out of a physical file using the OPNQRYF command, and only records with certain bits 'on' are to be selected, use the following technique.
Let's say file BINDATA contains a field FLAGS which is character, 10 bytes long. We wish to select records where the fifth byte of FLAGS is as follows: (1) The first bit (bit 0) is on and (2) either the fourth or fifth bit (bits 3 and 4) is on as illustrated below.
PGM DCL VAR(&QRYSLT) TYPE(*CHAR) LEN(256) CHGVAR VAR(&QRYSLT) VALUE('TEST0 *NE X''00'' *AND + TEST3 *NE X''00'' *OR TEST4 *NE X''00''') OVRDBF FILE(BINDATA) SHARE(*YES) OPNQRYF FILE((BINDATA)) QRYSLT(&QRYSLT) + MAPFLD((TESTBYTE '%SST(FLAGS 5 1)') + (TEST0 '%AND(TESTBYTE X''80'')') + (TEST3 '%AND(TESTBYTE X''10'')') + (TEST4 '%AND(TESTBYTE X''08'')')) CALL PGM(BINRPG) CLOF OPNID(BINDATA) DLTOVR FILE(BINDATA) ENDPGM
The three %AND functions isolate the three different bits we're testing. The %AND function does a logical AND operation. It compares two strings, bit by bit. If the same bit is on in both strings, it turns on the corresponding bit in the result string. Here are a couple of examples of how the first %AND would work with different values of TESTBYTE.
TESTBYTE 10111010 00011111 X'80' 10000000 10000000 -------- -------- TEST0 10000000 00000000
TEST0 will map to all zeros if bit 0 is off of the fifth byte of FLAGS. The same is true for the TEST3 field if bit 3 is off and the TEST4 field if bit 4 is off.
By the way, if the flag field is only one byte long, you don't have to use the first mapped field; you can just use the flag field in the other three mapped fields instead of TESTBYTE. The QRYSLT doesn't change.
LATEST COMMENTS
MC Press Online