The scope of an identifier refers to the PL unit in which the identifier can be referenced. The visibility of an identifier refers to the PL unit in which the identifier can be referenced without qualification.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition does not support this feature.
An 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 visible within 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 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 these identifiers are neither local nor global to the unit.
You cannot declare the same identifier twice in the same PL unit. Otherwise, an error will occur when you reference the duplicate identifier.
You can declare the same identifier in two different units. However, the two identifiers represent different objects. Modifying one does not affect the other.
You can specify unique names for labels and subroutines in the same scope to avoid confusion and errors.
-- 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 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.
