Cursor Idiosyncrasies
-
The optional
FOR UPDATE
clause can be included in theSELECT
statement, but its absence does not prevent successful execution of a positioned update or delete -
Care should be taken to ensure that the names of declared cursors do not conflict with any names used subsequently in statements for
AS CURSOR
clauses -
If the cursor is needed only to walk the result set, it is nearly always easier and less error-prone to use a
FOR SELECT
statement with theAS CURSOR
clause.Declared cursors must be explicitly opened, used to fetch data, and closed.The context variableROW_COUNT
has to be checked after each fetch and, if its value is zero, the loop has to be terminated.AFOR SELECT
statement does this automatically.Nevertheless, declared cursors provide a high level of control over sequential events and allow several cursors to be managed in parallel.
-
The
SELECT
statement may contain parameters. For instance:SELECT NAME || :SFX FROM NAMES WHERE NUMBER = :NUM
Each parameter has to have been declared beforehand as a PSQL variable, or as input or output parameters.When the cursor is opened, the parameter is assigned the current value of the variable.
Warning
|
Unstable Variables and Cursors
If the value of the PSQL variable used in the Note particularly that the behaviour may depend on the query plan, specifically on the indexes being used.Currently, there are no strict rules for this behaviour, and this may change in future versions of Firebird. |