Informix Error -662
-662 Loop variable variable-name specified more than once.
A loop variable was used more than once in a FOREACH statement.
Example of error:
FOREACH SELECT col1, col2 INTO var, var FROM tab -- error ... END FOREACH
Correction:
FOREACH SELECT col1, col2 INTO var1, var2 FROM tab ... END FOREACH
Oninit® Troubleshooting Guidance
Reasons / Common Causes
-662 fires when a FOREACH statement's INTO clause names the same variable more than once —
per the official guidance, each loop variable in the INTO clause must be distinct.
- The same variable name accidentally repeated in the
INTOclause, per the official guidance — the direct, only cause. - A copy-pasted
INTOclause where one placeholder variable name wasn't updated, leaving two columns mapped to the same variable.
Solutions / Resolution
- Replace the duplicate variable name with a unique one, per the official guidance's documented correction, so each selected column has its own distinct target variable.
- Review the full
INTOclause carefully if the duplication isn't the first two entries, since it can appear anywhere in a longer list.
Examples
The disallowed duplicate
FOREACH SELECT order_id, status, order_id INTO v_id, v_status, v_id FROM orders
...
END FOREACH;
-- -662: v_id specified twice
Corrected — unique variable names
DEFINE v_id, v_dup_id INT;
DEFINE v_status CHAR(10);
FOREACH SELECT order_id, status, order_id INTO v_id, v_status, v_dup_id FROM orders
...
END FOREACH;
Diagnostic Checks
- Scan the
FOREACH/INTOclause's variable list for repeated names, and assign each selected column its own distinct variable.
Related Errors / Related Topics
- -660 — "Loop variable variable-name cannot be modified." A related loop-variable
restriction, about modifying a
FORloop variable rather than reusing a name in aFOREACHclause.
Each column selected into a FOREACH's INTO clause needs its own distinct variable — check for
an accidentally repeated name.