References to SQL cursor names

Cursor names include the names of declared cursors and the names of cursor variables.

The name of an SQL cursor can be the same as the name of another SQL cursor declared in the same routine. This can occur when the two SQL cursors are declared in different compound statements.

The compound statement that contains the declaration of an SQL cursor determines the scope of that cursor name. A cursor name must be unique within the compound statement in which it is declared, excluding any declarations in compound statements that are nested within that compound statement (SQLSTATE 42734). A cursor name can only be referenced within the compound statement in which it is declared, including any compound statements that are nested within that compound statement. When there is a reference to a cursor name, the cursor that is declared in the innermost compound statement is the cursor that is used. For more information, see Compound SQL (inlined).

If the cursor constructor assigned to a cursor variable contains a reference to a local SQL variable, then any OPEN statement that uses the cursor variable must be within the scope where the local SQL variable was declared.