Building the QRYSLT parameter string of an OPNQRYF command is tricky business. You may be sick and tired of using so many concatenation operators (*CAT, *BCAT and *TCAT) and so many quotes, double quotes, quadruple quotes, etc. Further, when you use numeric variables, you must declare two variables: one in numeric format, the other in character format, since you cannot use the concatenation operators on numeric variables.
Take heart. Here's a technique you can use to do away with all this baggage, while improving readability of your CL programs. The technique consists of defining the desired-or final-QRYSLT string using a DCL statement, including placeholder characters for the variable values, and then "plugging" in the variables at the correct positions using the %SST (substring) function. In short, you'll be building a template.
Consider the example provided in 2. The CL program receives two parameters: &STATE (character) and &ZIP (decimal). The two CHGVAR commands plug the values of &STATE and &ZIP into the QRYSLT template, which is contained in variable &SEL. Then it's just a matter of running the OPNQRYF command.
Consider the example provided in Figure 2. The CL program receives two parameters: &STATE (character) and &ZIP (decimal). The two CHGVAR commands plug the values of &STATE and &ZIP into the QRYSLT template, which is contained in variable &SEL. Then it's just a matter of running the OPNQRYF command.
Editor's Note: This technique will work fine as long as the variables are fixed-length. Don't try to use this with variable-length variables.
TechTalk: QRYSLT With a Template
Figure 2 Sample QRYSLT template
Figure 2: Sample QRYSLT Template DCL VAR(&STATE) TYPE(*CHAR) LEN(2) DCL VAR(&ZIP) TYPE(*DEC) LEN(5 0) DCL VAR(&SEL) TYPE(*CHAR) LEN(33) + VALUE('STATE *EQ "xx" *AND ZIP *EQ nnnnn') + /* Template: .... ....1.... ....2.... ....3... */ CHGVAR VAR(%SST(&SEL 12 2)) VALUE(&STATE) CHGVAR VAR(%SST(&SEL 29 5)) VALUE(&ZIP) OVRDBF ... OPNQRYF FILE((...)) QRYSLT(&SEL) KEYFLD((...)) ENDPGM