0% found this document useful (0 votes)
244 views6 pages

Tugas3 1402018161

This document discusses nested PL/SQL blocks and variable scope. It provides examples of code with nested blocks and variables declared at different scopes. The key points are: - Variables declared within a block are only accessible (visible) within that block or nested blocks. - Variables declared in an outer block are accessible in nested inner blocks, but variables declared in inner blocks are not accessible in outer blocks. - To access a variable declared in an outer block from an inner block, a qualifier can be used to specify the block name where the variable is declared. - The document provides examples and questions to help understand how variable scope and visibility work with nested PL/SQL blocks.
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)
244 views6 pages

Tugas3 1402018161

This document discusses nested PL/SQL blocks and variable scope. It provides examples of code with nested blocks and variables declared at different scopes. The key points are: - Variables declared within a block are only accessible (visible) within that block or nested blocks. - Variables declared in an outer block are accessible in nested inner blocks, but variables declared in inner blocks are not accessible in outer blocks. - To access a variable declared in an outer block from an inner block, a qualifier can be used to specify the block name where the variable is declared. - The document provides examples and questions to help understand how variable scope and visibility work with nested PL/SQL blocks.
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/ 6

www.oracle.

com/academy

Gilang Raihansyah
1402018161

Database Programming with


PL/SQL 2-5: Writing PL/SQL
Executable Statements
Practice Activities
Vocabulary
Identify the vocabulary word for each definition below:

Converts values from one data type to another by using built-in


Explicit conversion functions.
Converts data types dynamically if they are mixed in a
Implicit conversion statement.
Try It / Solve It
1. Examine the following code and then answer the questions.

DECLARE
x
VARCHAR2(20);
BEGIN
x := '123' + '456' ;

DBMS_OUTPUT.PUT_LINE(
x); END;

A. What do you think the output will be when you run the above code?
Students may think that the answer might be: 579 or 123456 or Error

B. Now, run the code. What is the output?


579

C. In your own words, describe what happened when you ran the code. Did any
implicit conversions take place?
PL/SQL implicitly converted the VARCHAR2 values to the NUMBER format
and added them.

2. Write an anonymous PL/SQL block that assigns the programmer’s full name to a
variable, and then displays the number of characters in the name.
DECLARE
v_name VARCHAR2(50) :='Christian';
v_length_name PLS_INTEGER;
BEGIN
v_length_name := LENGTH(v_name);
DBMS_OUTPUT.PUT_LINE(v_length_name);
END;

3. Write an anonymous PL/SQL block that uses today's date and outputs it in the
format of ‘Month dd, yyyy’. Store the date in a DATE variable called my_date.
Create another variable of the DATE type called v_last_day. Assign the last day
of this month to v_last_day. Display the value of v_last_day.

DECLARE
my_date DATE := SYSDATE;
v_last_day DATE;
BEGIN
DBMS_OUTPUT.PUT_LINE(TO_CHAR(my_date, 'Month dd, yyyy'));
v_last_day := LAST_DAY(my_date);
DBMS_OUTPUT.PUT_LINE(v_last_day);
END;

4. Modify the program created in question 3 to add 45 days to today’s date and then
calculate and display the number of months between the two dates.
DECLARE
my_date DATE := SYSDATE;
new_date DATE;
v_months_between NUMBER;
BEGIN
new_date := my_date + 45;
v_months_between := MONTHS_BETWEEN(new_date,my_date);
DBMS_OUTPUT.PUT_LINE(v_months_between);
END;

5. Examine the following code and then answer the questions.

DECLARE
x
NUMBER(6);
BEGIN
x := 5 + 3 * 2 ;
DBMS_OUTPUT.PUT_LINE(
x); END;

A. What do you think the output will be when you run the above code?

Students may think that the answer might be: 16 or 11

B. Now run the code. What is the output?

11

C. In your own words, explain the results.


The order of operations tells you that multiplication takes precedence
over addition. Therefore, 3 * 2 is executed before 5 is added.

6. Examine the following code and then answer the question.

DECLARE
v_number
NUMBER;
v_boolean
BOOLEAN;
BEGIN
v_number := 25;
v_boolean := NOT(v_number > 30);
END;

What value is assigned to v_boolean?


TRUE. The condition (v_number > 30) is FALSE, and NOT FALSE = TRUE.

7. List two drawbacks to relying on implicit data type conversions.

• Implicit datatype conversion can have a negative impact on performance


• Implicit conversion depends on the context in which it occurs and may
not work the same way in every case
www.oracle.com/academy

Database Programming with PL/SQL


2-6: Nested Blocks and Variable Scope
Practice Activities
Vocabulary
Identify the vocabulary word for each definition below.

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


Qualifier variables that have scope, but are not visible.
Consists of all the blocks in which the variable is either local (the
Variable scope declaring block) or global (nested blocks within the declaring block) .
The portion of the program where the variable can be accessed
Variable visibility 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;
A. The value of weight at position 1 is:
2

B. The value of new_locn at position 1 is:


Western Europe

C. The value of weight at position 2 is:


601
D. The value of message at position 2 is:
Product 10012 is in stock
E. The value of new_locn at position 2 is:
Out of range – new_locn is undefined in the outer block.

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?
Because although both declarations of v_job are in scope and in the inner
block,
the outer block’s declaration is not visible.
B. Why does the outer block display the job_id of employee 100, not employee
103?
Because the inner block’s declaration is out of scope in the outer block.

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

<<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>>
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(outer_block.v_employee_id||
' is a '||outer_block.v_job);
END;
DBMS_OUTPUT.PUT_LINE(v_employee_id||' is a '||v_job);
END;

You might also like