The scope of an identifier refers to the area within which the identifier can be referenced in a PL unit. The visibility of an identifier refers to the area within which the identifier can be referenced without qualification in a PL unit.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only MySQL mode.
The identifier is local to the PL unit in which it is declared. If the unit has subunits, the identifier is global to those subunits.
If a subunit redeclares a global identifier, both identifiers are available within the subunit, but only the local identifier is visible. To reference the global identifier, the subunit must qualify it with the name of the declaring unit. If the unit does not have a name, the subunit cannot reference the global identifier.
A PL unit cannot reference identifiers declared in other units at the same level, because those identifiers are not local or global.
You cannot declare the same identifier twice in the same PL unit. Otherwise, an error is returned when the identifier is referenced.
You can declare the same identifier in two different units. However, the two objects represented by the identifiers are different. Modifying one does not affect the other.
Specify unique names for labels and subroutines in the same scope to avoid confusion and exceptions.
-- Outer block
obclient> DECLARE
x CHAR; -- x (CHAR) scope begins
y FLOAT; -- y scope begins
BEGIN
-- x (CHAR) and y are visible
-- First subblock:
DECLARE
x INTEGER; -- x (INTEGER) scope begins
z FLOAT; -- z scope begins
BEGIN
-- x (INTEGER), y, and z are visible
NULL;
END; -- x (INTEGER) and z scopes end
-- Second subblock:
DECLARE
w FLOAT; -- w scope begins
BEGIN
-- x (CHAR), y, and w are visible
NULL;
END; -- w scope ends
-- x (CHAR) and y are visible
END; -- x (CHAR) and y scopes end
/
Query OK, 0 rows affected
This example shows the scopes and visibility of multiple identifiers. The first subblock redeclares the global identifier x. To reference the global variable y, the first subblock must qualify it with the name of the outer block. However, the outer block does not have a name. Therefore, the first subblock cannot reference the global variable x and can only reference its local variable x. Because the subblocks are at the same level, the first subblock cannot reference w, and the second subblock cannot reference z.