OPNQRYF Templates
As an OPNQRYF beginner, I stumbled along making the usual mistakes, getting acquainted with error message CPD3129 ("Missing operand on expression...") and often getting lost in quotes and *BCATs. However, I have since written a number of CL programs that use the OPNQRYF command and now use the following technique:
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 %SST in a CHGVAR command to replace the X's and 9's with the actual values I require in my QRYSLT expression. See 3. In the example illustrated in 3, the user is expected to give &STATE a two- character value and &ZIP a five-digit value-which the program receives as parameters. When the CHGVAR commands are executed, the computer "sees" the &QRYSLT variable as shown in 4.
2. Then I use %SST in a CHGVAR command to replace the X's and 9's with the actual values I require in my QRYSLT expression. See Figure 3. In the example illustrated in Figure 3, the user is expected to give &STATE a two- character value and &ZIP a five-digit value-which the program receives as parameters. When the CHGVAR commands are executed, the computer "sees" the &QRYSLT variable as shown in Figure 4.
Notice the placement of the comment containing a scale in 3. 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. 3 illustrates this.
Notice the placement of the comment containing a scale in Figure 3. 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. Figure 3 illustrates this.
- Susan Berrey
TechTalk: OPNQRYF Templates
Figure 3 Coding QRYSLT With Templates
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
TechTalk: OPNQRYF Templates
Figure 4 QRYSLT Value After Substitutions
/* ....+... 1 ...+... 2 ...+... 3 ...+ */ QRYSLT('STATE *EQ "CA" *OR ZIP *EQ 92008')
LATEST COMMENTS
MC Press Online