Scope and visibility- pl/sql, PL-SQL Programming

Scope and Visibility

The References to an identifier are resolved according to its visibility and scope. The scope of an identifier is that area of a program unit (subprogram, block, or package) from which you can reference the identifier. An identifier is visible only in the areas from which you can reference the identifier using an unqualified name. The Figure shows the visibility and scope of a variable named x that is declared in an enclosing block, and then re-declared in a sub-block.

The Identifiers declared in a PL/SQL block are considered local to that block and global to all its sub-blocks. If a global identifier is re-declared in a sub-block, both the identifiers remain in the scope. Within the sub-block, though, only the local identifier is visible as you must use a qualified name to reference the global identifier.

Though you cannot declare an identifier twice in the similar block, you can declare the same identifier in two various blocks. The two items represented by the identifier are discrete, and any change in one does not affect the other. Though, a block cannot reference the identifiers declared in other blocks at the similar level as these identifiers are neither global nor local to the block.

 

2006_Scope and Visibility.jpg

 

                                          Figure: Scope and Visibility




The example below describes the scope rules. Note that the identifiers declared in one sub-block cannot be referenced in another sub-block. That is because the block cannot reference the identifiers declared in another blocks nested at similar level.


DECLARE
a CHAR;
b REAL;
BEGIN

-- identifiers available here: a (CHAR), b
DECLARE
a INTEGER;
c REAL;
BEGIN
-- identifiers available here: a (INTEGER), b, c
END;
DECLARE
d REAL;
BEGIN
-- identifiers available here: a (CHAR), b, d
END;
-- identifiers available here: a (CHAR), b
END;


Remember that the global identifiers can be re-declared in a sub-block, in that case the local declaration prevails and the sub-block cannot reference the global identifier unless you use a qualified name. The qualifier can be the label of an enclose block, as the example below shows:


<>
DECLARE
birthdate DATE;
BEGIN
DECLARE
birthdate DATE;
BEGIN
...
IF birthdate = outer.birthdate THEN...


As the next illustration shown below, the qualifier can also be the name of an enclosing subprogram:


PROCEDURE check_credit (...) IS
rating NUMBER;
FUNCTION valid (...) RETURN BOOLEAN IS
rating NUMBER;
BEGIN
...
IF check_credit.rating < 3 THEN...


Though, within the same scope, a label and a subprogram cannot have the similar name.

Posted Date: 10/3/2012 5:01:20 AM | Location : United States







Related Discussions:- Scope and visibility- pl/sql, Assignment Help, Ask Question on Scope and visibility- pl/sql, Get Answer, Expert's Help, Scope and visibility- pl/sql Discussions

Write discussion on Scope and visibility- pl/sql
Your posts are moderated
Related Questions
Using Cursor Attributes To process the SQL data manipulation statements, the SQL engine must opens an implicit cursor named SQL. This cursor's attributes (%FOUND, %NOTFOUND, %

Fetching Across Commits The FOR UPDATE clauses acquire exclusive all row locks. All rows are locked when you open the cursor, and when you commit your transaction they are unl

Closing a Cursor Variable The CLOSE statement disables the cursor variable. After that, the related result set is undefined. The syntax for the same is as shown below: CLOS

Relational Algebra - SQL It describes some operators, that together constitute an algebra that is not only relationally complete but also irreducibly so (very nearly- apart f

Using TRIM This process has two forms. The TRIM removes an element from the end of the collection. The TRIM(n) removes the n elements from the end of the collection. For e.g.

Data Types in SQL SQL's concept does not differ significantly from that defined in the theory book, apart from that business concerning NULL. However, the theory book equates

%ROWCOUNT When its cursor or cursor variable is opened, the %ROWCOUNT is zeroed. Before the first fetch, the %ROWCOUNT yields 0. Afterward, it yields the number of rows fetche

Expression: This is a randomly complex combination of constants, variables, literals, operators, & function calls. The simplest expression consists of a single variable. If th

Rephrase Conditional Control Statements When computing a logical expression, the PL/SQL uses short-circuit evaluation. That is, the PL/SQL stops evaluating the expression as s

EXIT Statement   You can use the EXIT statement to exit a loop. The EXIT statement has 2 forms: the conditional EXIT WHEN and the unconditional EXIT. With the either form, you