RPG (IBM i / AS/400) · DB2 SQLCODE
What does SQLCODE -518 - Statement Not in a Usable Prepared State mean in RPG (IBM i / AS/400)?
An EXECUTE or OPEN referenced a statement name from a PREPARE that either failed, was never issued, or prepared a statement type that can't be run that way - for example trying to EXECUTE a select-statement instead of opening a cursor for it.
An EXECUTE or OPEN referenced a statement name from a PREPARE that either failed, was never issued, or prepared a statement type that can't be run that way - for example trying to EXECUTE a select-statement instead of opening a cursor for it.
Check the SQLCODE from the PREPARE before trying to EXECUTE or OPEN against it, and use DECLARE CURSOR ... FOR the prepared statement plus OPEN for any dynamic SELECT rather than EXECUTE.