What’s wrong is this statement:<pre>
DELETE FROM mylib/myfile WHERE myfile = ‘&USER'</pre>
You don’t have the USER replacement variable anywhere in that statement.
It looks like you want to have it as part of the WHERE clause, but you actually have just a literal constant in the place where the the replacement variable should be. Compare what you have to this statement:<pre>
DELETE FROM mylib/myfile WHERE myfile = &USER</pre>
The second statement doesn’t have quotes, so the literal constant is gone and the replacement variable can be recognized by the statement processor. The statement processor ignores everything inside of quotes because literals are always ignored by essentially all languages.
If you had a variable in RPG named USER and had a statement like MOVE ‘USER’ MYUSER, would you expect the value of the USER variable to show up in MYUSER? Or would you expect MYUSER simply to receive the value ‘USER’? In CL, you might have CHGVAR &MYUSER ‘&USER’. What will &MYUSER contain after that statement runs?
In a QM query statement, you need to add the quotes at run-time. You can’t have them be part of the compiled statement if you need them to enclose a replacement variable. If you compile the QM query with the quotes in place, the variable becomes a simple constant.
Fortunately, any part of the QM statement can be replaced by a replacement variable — even quote marks.
There are two general ways to get the quote marks into your statement. Your CL program can concatenate quotes into the variable value that you pass into the query. Or you can use a separate replacement variable that just holds a quote mark that you put in different places in the QM statement.
For the first case, you might have a couple new variables in your CL program that look like this:<pre>
DCL &qUSER *CHAR 12
DCL &q *CHAR 1 VALUE( ”” )</pre>
The new &qUSER variable needs to be long enough to allow you to add quotes at the beginning and end. Then you might have a statement like this:<pre>
CHGVAR &qUSER VALUE( &q *cat &USER *tcat &q )</pre>
Your STRQMQRY would then pass the value of &qUSER into the USER replacement variable.
For the second case, you would change your QM statement to look like this:<pre>
DELETE FROM mylib/myfile WHERE myfile = &q&USER&q</pre>
There are now two places where a new replacement variable is going to be inserted. The new replacement variable is &q. (I used a lower-case “q” just to make it stand out a little. Be aware that replacement variable names actually are case-sensitive.)
To make that work, you need to have your STRQMQRY statement pass another value in:<pre>
SETVAR((USER &USER) ( ‘q’ ”” )</pre>
Now, wherever the &q appears in your QM statement, it will be replaced with a single-quote mark. (The value in the SETVAR() parameter is made up of four single-quote marks.)
In short, choose one alternative or the other. Either add quote marks to each of the actual character values that you pass in, or use a replacement variable to put single-quote marks at all of the places where replacement variables need to be surrounded by quotes.
I almost always use the second method. I can use the replacement variable in many places in the QM statement, and it has very minimal impact on any CL program that uses it.