Informix Error -765: Cannot EXECUTE a statement that has been DECLAREd.
Cause and resolution
Cannot EXECUTE a statement that has been DECLAREd.
You cannot execute a prepared statement that has been declared. However, to specify output variables for a prepared singleton SELECT statement, use EXECUTE INTO (or EXECUTE PROCEDURE INTO) instead of executing DECLARE, OPEN, and FETCH statements.
Oninit® Troubleshooting Guidance
Reasons / Common Causes
-765 fires when EXECUTE is issued against a prepared statement that was instead DECLAREd as a
cursor — per the official guidance, a declared statement follows the cursor lifecycle
(DECLARE/OPEN/FETCH/CLOSE), not the EXECUTE lifecycle; the two are mutually exclusive
ways of running a prepared statement.
- A prepared statement
DECLAREd as a cursor, thenEXECUTEd instead ofOPENed/FETCHed, per the official guidance — the direct, only cause. - Code written for a singleton
SELECT(one row, no cursor needed) that used theDECLARE/OPEN/FETCHcursor pattern instead of the simplerEXECUTE INTOform, then mixed the two patterns by also tryingEXECUTE.
Solutions / Resolution
- Use
OPEN/FETCH/CLOSEfor a statement that wasDECLAREd, per the official guidance — follow the cursor lifecycle consistently. - For a singleton
SELECTwith output variables, useEXECUTE ... INTO(orEXECUTE PROCEDURE ... INTO) instead of declaring a cursor at all, per the official guidance — this is the simpler, more direct mechanism for the one-row case, avoiding the cursor lifecycle entirely.
Examples
The disallowed mix
EXEC SQL PREPARE stmt_id FROM 'SELECT status FROM orders WHERE order_id = ?';
EXEC SQL DECLARE cur1 CURSOR FOR stmt_id;
EXEC SQL EXECUTE stmt_id INTO :status USING :order_id;
/* -765: stmt_id was DECLAREd as a cursor, not meant for EXECUTE */
Corrected — follow the cursor lifecycle
EXEC SQL PREPARE stmt_id FROM 'SELECT status FROM orders WHERE order_id = ?';
EXEC SQL DECLARE cur1 CURSOR FOR stmt_id;
EXEC SQL OPEN cur1 USING :order_id;
EXEC SQL FETCH cur1 INTO :status;
EXEC SQL CLOSE cur1;
Or corrected — EXECUTE INTO for a singleton SELECT, no cursor needed
EXEC SQL PREPARE stmt_id FROM 'SELECT status FROM orders WHERE order_id = ?';
EXEC SQL EXECUTE stmt_id INTO :status USING :order_id;
Diagnostic Checks
- Check whether the prepared statement was
DECLAREd as a cursor, and if so, useOPEN/FETCH/CLOSErather thanEXECUTE— or switch toEXECUTE ... INTOand drop theDECLAREentirely if it's a singletonSELECT.
Related Errors / Related Topics
No closely related error codes are cross-referenced for -765 in this set yet.
DECLARE and EXECUTE are two separate, mutually exclusive ways to run a prepared statement —
use EXECUTE ... INTO for a singleton SELECT instead of mixing the two.