Nested compound statements and scope of names

Nested compound statements can be used within an SQL routine to define the scope of SQL variable declarations, cursors, condition names, and condition handlers.

In addition, labels have a defined scope in the context of nested compound statements. However, the rules for name spaces and how non-unique names can be referenced, differs depending on the type of name. The following table summarizes these differences:
Table 1. Scope and qualification of names within nested compound statements
Type of name Name can be qualified Name must be unique within Name can be referenced within
SQL variable Yes. The name can be qualified with the label of the compound statement in which the variable is declared the compound statement in which it is declared, excluding any declarations in compound statements that are nested within that compound statement The compound statement in which it is declared, including any compound statements that are nested within that compound statement.

When multiple SQL variables are defined with the same name, a label can be used to explicitly refer to a specific variable that is not the most local in scope

condition name No the compound statement in which it is declared, excluding any declarations in compound statements that are nested within that compound statement The compound statement in which it is declared, including any compound statements that are nested within that compound statement.

Condition names can be used in the declaration of a condition handler, or in a SIGNAL or RESIGNAL statement.

If multiple conditions are defined with the same name, there is no way to explicitly refer to the condition that is not the most local in scope.

cursor name No the routine The compound statement in which it is declared, including any compound statements that are nested within that compound statement.

If the cursor is defined as a result set cursor, the invoking application can access the result set.

label No the compound statement that defined the label, including any definitions in compound statements that are nested within that compound statement The compound statement in which it is defined, including any compound statements that are nested within that compound statement.

Use a label to qualify the name of an SQL variable or as the target of a GOTO, LEAVE, or ITERATE statement, subject to the rules for these statements.