The search condition of an IF or WHILE statement contains a QUERY statement. This is not allowed.
Execute the QUERY statement outside of the IF or WHILE statement, and save the results in program variables. Then use these results in the IF or WHILE statement.