The scope of an identifier refers to the area within which the identifier can be referenced. The visibility of an identifier refers to the area within which the identifier can be referenced without qualification.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only MySQL 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 available in the subunit. However, only the local identifier is visible. To reference a 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.
You cannot declare the same identifier twice in the same PL unit. Otherwise, an error will be returned when you reference the repeated 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.
Specify unique names for labels and subprograms in the same scope to avoid confusion and exceptions.
-- 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. Since the subblocks are at the same level, the first subblock cannot reference w, and the second subblock cannot reference z.