The scope of an identifier is the area in which it can be referenced. The visibility of an identifier is the area in which it can be referenced without qualification.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only MySQL-compatible mode.
An identifier is local to the PL unit in which it is declared. If the unit has subunits, the identifier is global to the subunits.
If a subunit redeclares a global identifier, both identifiers are visible in the subunit. However, only the local identifier is visible. To reference the global identifier, the subunit must qualify it by using the name of the declaring unit. If the unit has no name, the subunit cannot reference the global identifier.
A PL unit cannot reference identifiers declared in other units at the same level because these identifiers are not local or global.
An identifier cannot be declared twice in the same PL unit. Otherwise, an error will occur when the identifier is referenced.
You can declare the same identifier in two different units. However, the two identifiers represent different objects. Changing one does not affect the other.
To avoid confusion and exceptions, specify unique names for labels and subprograms in the same scope.
-- Outer block
obclient> DECLARE
x CHAR; -- x (CHAR) scope starts
y FLOAT; -- y scope starts
BEGIN
-- x (CHAR) and y are visible
-- First subblock:
DECLARE
x INTEGER; -- x (INTEGER) scope starts
z FLOAT; -- z scope starts
BEGIN
-- x (INTEGER), y, and z are visible
NULL;
END; -- x (INTEGER) and z scopes end
-- Second subblock:
DECLARE
w FLOAT; -- w scope starts
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 scope 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 by using the name of the outer block. However, the outer block has no 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.