Informix Error -538: Cursor cursor-name has already been declared.
Cause and resolution
Cursor cursor-name has already been declared.
The cursor that is named in this DECLARE statement has also been named in a DECLARE statement that was executed earlier. Possibly the same DECLARE is being executed in a loop, or possibly the program meant to execute a FREE statement for this cursor but did not. Review the program logic to ensure that it executes only a single DECLARE for each cursor (except after freeing one).
Oninit® Troubleshooting Guidance
Reasons / Common Causes
-538 fires when a DECLARE CURSOR statement names a cursor that's already been declared earlier
in the same session and hasn't since been freed.
- The same
DECLAREstatement executed twice, per the official guidance — possibly because it sits inside a loop and runs on every iteration instead of once. - A missing
FREE cursor-namebefore a secondDECLAREfor the same cursor name, per the official guidance — the program meant to free it first but didn't. - A retry/error-handling path that re-runs the
DECLAREafter a failure, without first freeing the cursor from the original attempt.
Solutions / Resolution
- Review the program logic to ensure only a single
DECLAREexecutes per cursor, per the official guidance, except immediately after freeing one. - Move the
DECLAREoutside a loop if it's currently inside one and only needs to run once, withOPEN/FETCH/CLOSEhandling the per-iteration work instead. - Add an explicit
FREE cursor-namebefore any code path that might re-declare the same cursor, particularly retry logic.
Examples
DECLARE inside a loop by mistake
FOR i = 1 TO 3
DECLARE order_cursor CURSOR FOR SELECT * FROM orders;
-- -538 on the second iteration: order_cursor is already declared
END FOR
Corrected — DECLARE once, OPEN/CLOSE per iteration if needed
DECLARE order_cursor CURSOR FOR SELECT * FROM orders;
FOR i = 1 TO 3
OPEN order_cursor;
-- ... fetch loop ...
CLOSE order_cursor;
END FOR
FREE order_cursor;
Diagnostic Checks
- Check whether the
DECLAREstatement sits inside a loop or a retried code path that could execute it more than once without an interveningFREE. - Confirm a
FREE cursor-nameexists on every path that might lead to a secondDECLAREfor the same cursor name.
Related Errors / Related Topics
- -507 — "Cursor cursor-name not found." The inverse situation: a positioned UPDATE/DELETE naming a cursor that isn't currently declared, rather than a DECLARE repeating one that already is.
Usually a DECLARE re-executed without an intervening FREE — check for it inside a loop or a
retry path first.