Informix Error -401
-401 Fetch attempted on NULL cursor.
This FETCH statement passed a cursor data structure that is invalid or that is a null pointer. Possibly the cursor has been freed with the FREE statement, or possibly an automatic re-prepare has been attempted while opening the cursor but that operation failed, leaving the cursor unavailable, or possibly the cursor data structure has been overwritten in memory.
Version 5.0 or later database servers do not return this error code. See errors -267 and -404.
Oninit® Troubleshooting Guidance
Reasons / Common Causes
-401 is a more severe cursor-state error than -400: rather than a cursor simply not being open,
the cursor data structure itself is invalid or null. Per the official guidance, database servers
version 5.0 and later don't return this specific error at all — the same underlying conditions
instead surface as -267 or -404 on those newer versions, so encountering -401 itself is a
signal about the server version in use.
- The cursor was explicitly freed with a
FREEstatement, and aFETCHwas attempted against it afterward. - An automatic re-prepare failed while opening the cursor — a background mechanism that re-validates a prepared statement, which didn't succeed, leaving the cursor structure invalid.
- The cursor data structure was corrupted in memory — a more serious, rarer condition, potentially pointing to an application-level memory bug (in embedded SQL/ESQL client code) rather than anything server-side.
- Working on a server version older than 5.0, where this specific error code is still in
active use; on 5.0+ servers, look for
-267/-404instead when troubleshooting equivalent conditions.
Solutions / Resolution
- Check whether the cursor was freed (
FREE) before theFETCH— don't attempt to use a cursor after freeing it. - Check for an automatic re-prepare failure around the
OPENstep, which may itself surface an earlier, more specific error worth investigating first. - On server versions 5.0 and later, look at
-267/-404instead if troubleshooting an equivalent-seeming condition, since-401itself won't be returned there. - If memory corruption is suspected (in embedded SQL/ESQL client code), review the application's own memory management around cursor handles, since this can point to a client- side bug rather than a server-side one.
Examples
Confirming the cursor wasn't freed before fetching
DECLARE curs1 CURSOR FOR SELECT * FROM orders;
OPEN curs1;
FETCH curs1;
FREE curs1;
FETCH curs1;
-- -401 (pre-5.0): curs1 was freed before this fetch
Diagnostic Checks
- Check for a
FREEstatement on the cursor before the failingFETCH. - Check the server version — on 5.0+, this exact error won't appear; look for
-267/-404instead. - Review embedded SQL/ESQL client code for memory-management issues around cursor handles if corruption is suspected.
Related Errors / Related Topics
- -400 — "Fetch attempted on unopen cursor." The more common, less severe sibling — a cursor simply not currently open, versus this error's invalid/corrupted cursor structure.
On server versions 5.0 and later, this exact error code isn't returned — the equivalent
conditions surface as -267 or -404 instead, so check the server version first.