0% found this document useful (0 votes)
5 views3 pages

PLSQL 2 6 Practice

The document provides practice activities on nested blocks and variable scope in PL/SQL. It includes definitions of key vocabulary such as qualifier, scope, and scope region, along with exercises to evaluate variable values in nested blocks. Additionally, it explains how to modify code to ensure inner blocks use outer block variables effectively.

Uploaded by

Manuel Ovando
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

PLSQL 2 6 Practice

The document provides practice activities on nested blocks and variable scope in PL/SQL. It includes definitions of key vocabulary such as qualifier, scope, and scope region, along with exercises to evaluate variable values in nested blocks. Additionally, it explains how to modify code to ensure inner blocks use outer block variables effectively.

Uploaded by

Manuel Ovando
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Database Programming with

PL/SQL 2-6: Nested Blocks and


Variable Scope Practice Activities
Vocabulary
Identify the vocabulary word for each definition below.

Qualifier A name given to a block of code which allows access to the


variables that have scope, but are not visible.
Scope Consists of all the blocks in which the variable is either local
(the declaring block) or global (nested blocks within the
declaring block) .
Scope region The portion of the program where the variable can be
accessed without using a qualifier.

Try It / Solve It
1. Evaluate the PL/SQL block below and determine the value of each of the
following variables according to the rules of scoping.

DECLARE
weight NUMBER(3) := 600;
message VARCHAR2(255) := 'Product
10012'; BEGIN

DECLARE
weight NUMBER(3) := 1;
message VARCHAR2(255) := 'Product
11001'; new_locn VARCHAR2(50) :=
'Europe';
BEGIN
weight := weight + 1;
new_locn := 'Western ' || new_locn;
-- Position 1 --
END;

weight := weight + 1;
message := message || ' is in stock';
-- Position 2 --
END;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
A. The value of weight at position 1 is:
weight = 2 at Position 1.

B. The value of new_locn at position 1 is:


new_locn = 'Western Europe' at Position 1.

C. The value of weight at position 2 is:


weight = 601 at Position 2.

D. The value of message at position 2 is:


message = 'Product 10012 is in stock' at Position 2.

E. The value of new_locn at position 2 is:


new_locn is not defined at Position 2.

2. Enter and run the following PL/SQL block, which contains a nested block. Look at the
output and answer the questions.

DECLARE
v_employee_id employees.employee_id
%TYPE; v_job employees.job_id
%TYPE;
BEGIN
SELECT employee_id, job_id INTO v_employee_id, v_job
FROM employees
WHERE employee_id = 100;

DECLARE
v_employee_id employees.employee_id
%TYPE; v_job employees.job_id%TYPE;
BEGIN
SELECT employee_id, job_id INTO v_employee_id,
v_job FROM employees
WHERE employee_id = 103;
DBMS_OUTPUT.PUT_LINE(v_employee_id || ' is a(n) ' || v_job);
END;

DBMS_OUTPUT.PUT_LINE(v_employee_id || ' is a(n) ' ||


v_job); END;

A. Why does the inner block display the job_id of employee 103, not employee 100?
The inner block displays employee 103's job_id because it uses local variables in the inner block
that are separate from the outer block's variables.

B. Why does the outer block display the job_id of employee 100, not employee 103?
The outer block displays employee 100’s job_id because it is using the outer block’s variables, which
were populated by the first SELECT statement for employee 100.

2
C. Modify the code to display the details of employee 100 in the inner block. Use block labels.

To ensure that the inner block uses the outer block’s variables, we can remove the re-declaration of the variables
v_employee_id and v_job inside the inner block. This way, the inner block will use the same variables from the outer
block.

DECLARE
v_employee_id employees.employee_id%TYPE;
v_job employees.job_id%TYPE;
BEGIN
SELECT employee_id, job_id INTO v_employee_id, v_job FROM employees
WHERE employee_id = 100;

-- Inner block using outer block variables


BEGIN
DBMS_OUTPUT.PUT_LINE(v_employee_id || ' is a(n) ' || v_job);
END;

DBMS_OUTPUT.PUT_LINE(v_employee_id || ' is a(n) ' || v_job);


END;

Now, both the inner and outer blocks will output the details of employee 100 because they are using the same
variables defined in the outer block

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

You might also like