Nested Blocks and Variable Scope. Copyright 2007, Oracle. All rights reserved.



Similar documents
Handling Exceptions. Copyright 2008, Oracle. All rights reserved.

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

Handling Exceptions. Schedule: Timing Topic 45 minutes Lecture 20 minutes Practice 65 minutes Total

Handling Exceptions. Schedule: Timing Topic. 45 minutes Lecture 20 minutes Practice 65 minutes Total

Oracle For Beginners Page : 1

Handling PL/SQL Errors

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

ORACLE 9I / 10G / 11G / PL/SQL COURSE CONTENT

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Duration Vendor Audience 5 Days Oracle Developers, Technical Consultants, Database Administrators and System Analysts

Job Posting. Customize your posting and hire the right candidate sooner. Let s get started. Log into your Workopolis Employer Account.

When an exception occur a message which explains its cause is received. PL/SQL Exception message consists of three parts.

Writing Control Structures

Oracle 11g PL/SQL training

Answers to the Try It Yourself Sections


Database Programming with PL/SQL: Learning Objectives

Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

Handling PL/SQL Errors

Course -Oracle 10g SQL (Exam Code IZ0-047) Session number Module Topics 1 Retrieving Data Using the SQL SELECT Statement

Oracle Database: Develop PL/SQL Program Units

Bullet Proof Your PL/SQL

Oracle PL/SQL Best Practices

PL/SQL Overview. Basic Structure and Syntax of PL/SQL

IT Quick Reference Guides Performing Mail Merges in Word 2010

STUDY OF PL/SQL BLOCK AIM: To Study about PL/SQL block.

Northwestern State University. Credit Cards. Information you can use..

Using Microsoft PowerPoint Software on a SMART Board Interactive Whiteboard

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Job Posting. Log into your Workopolis Employer Account. From your Employer Dashboard: Click the Post a Job button

What is the value of SQL%ISOPEN immediately after the SELECT statement is executed? Error. That attribute does not apply for implicit cursors.

Oracle Database: SQL and PL/SQL Fundamentals

Nursing Fellowship Application form 2008

MDHearingAidAIR Diagram

Using Microsoft Access

Oracle Database 10g: Program with PL/SQL

OPP ODTUG Kaleidoscope. An ODTUG SP* Oracle PL/SQL Programming Conference. WOW-Wide Open World, Wide Open Web!

Oracle Database: SQL and PL/SQL Fundamentals NEW

Global Variables. However, when global variables are used in a function block or control modules, they must be declared as external

Creating and Merging a Database in Design Pro (Windows) Merging a Database via ODBC in Design Pro (Windows)

REPUBLIC OF SOUTH AFRICA FORM 11

Transactionality and Fault Handling in WebSphere Process Server Web Service Invocations. version Feb 2011

Oracle Database: SQL and PL/SQL Fundamentals

ONLINE APPLICATION GUIDE

Welcome to the University of Central Florida Online Employment System Applicant Tutorial

GENERAL PROGRAMMING LANGUAGE FUNDAMENTALS

The different kinds of variables in a Java program

2. Which of the following declarations is invalid? Mark for Review (1) Points

California Team Decision Making Application (TDM CA v.2.3) Technical Guide

Business Application

2015/16. When to apply. Evidence. Payment. Frequently asked questions by parents. SF_England /SFEFILM. SFEngland

Introduction Purpose... 4 Scope... 4 Manitoba ehealth Incident Management... 4 Icons... 4

Form No. 1C APPLICATION FORM FOR INDIAN PASSPORT AT AN INDIAN MISSION/POST. Passport for New Born Baby

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Why programming extensions? to program complex update transactions. where referential integrity is not addressed by data definition

Operating Manual QUESTOR

In-year common application form (icaf)

Concepts and terminology in the Simula Programming Language

novdocx (en) 11 December 2007 VIDistribution Lists, Groups, and Organizational Roles

d)execute and test the PL/SQL block for the countries with the IDs CA, DE, UK, US.

CMS/ Custom Maintenance Software. Operating Manual

ESSENTIAL QUESTION How can you factor expressions of the form ax 2 + bx + c?

REFERENCE CHECK APPLICATION FORM

Displaying Data from Multiple Tables

Guest Welcome Screen PRO User Guide

THE BASICS Changing Your Name and Social Security Number in New York State

Plots, Curve-Fitting, and Data Modeling in Microsoft Excel

Child Benefit claim D D M M Y Y D D M M Y Y. Postcode PERSONAL DETAILS

Completing an online application for school admissions with the City and County of Swansea

Getting Started Configuring Your Computer Network Settings

How Do I Apply For Financial Aid?

Oracle 10g PL/SQL Training

D B M G Data Base and Data Mining Group of Politecnico di Torino

Q. Can I use my Short Term Disability (STD) policy concurrent with banked paid time during a FMLA leave?

Microsoft Excel 2007 Consolidate Data & Analyze with Pivot Table Windows XP

APPLICATION FORM for the academic year:

First comes baby then comes paperwork!

Post Code: Post Code:

Creating a Database in Access

SuccessFactors Learning: Learning Needs Management

Using Excel to create a student database

OBJECTIVES. The BIG Idea. How do I register for the ACT? What do I need to know for the test day? ACT Registration

Serving the Future with Your Gifts Today

POS Connector Installation and Setup Guide

Integration loan application form

Parent s Consent Form for a School-Aged or Young Child

Lot: a group of product that is unique to time, date and location of manufacture.

Applying to Be a CTE Transitions Student

Public Liability Claim Form

Instructions. Outlook (Windows) Mail (Mac) Webmail Windows Live Mail iphone 4, 4S, 5, 5c, 5s Samsung Galaxy S4 BlackBerry

Financial Support Office Undergraduate Scholarship Application Form

SECTION 0.6: POLYNOMIAL, RATIONAL, AND ALGEBRAIC EXPRESSIONS

Connecting to Secure Wireless (iitk-sec) on Fedora

AA 50 Plus Life Insurance

Netigate User Guide. Setup Introduction Questions Text box Text area Radio buttons Radio buttons Weighted...

Dell P Series Monitor VESA Mounting Bracket Installation Instructions

Anytime Learning Orientation Guide

Microsoft Outlook 2007 Calendar Features

Google Drive lets you store and share all your stuff, including documents, videos, images and other files that are important to

Transcription:

What Will I Learn? In this lesson, you will learn to: Understand the scope and visibility of variables Write nested blocks and qualify variables with labels Understand the scope of an exception Describe the effect of exception propagation in nested blocks 2

Why Learn It? A large, complex block may be hard to understand. We can break it down into smaller blocks that are nested one inside the other, making the code easier to read and correct. When you nest blocks, the declared variables may or may not be available depending on their scope and visibility. You can make invisible variables available by using labels. 3

Nested Blocks The example shown in the slide has an outer (parent) block (illustrated in normal text) and a nested (child) block (illustrated in bold text). The variable v_outer_variable is declared in the outer block and the variable v_inner_variable is declared in the inner block. v_outer_variable VARCHAR2(20):='GLOBAL VARIABLE'; v_inner_variable VARCHAR2(20):='LOCAL VARIABLE'; DBMS_OUTPUT.PUT_LINE(v_inner_variable); DBMS_OUTPUT.PUT_LINE(v_outer_variable); DBMS_OUTPUT.PUT_LINE(v_outer_variable); 4

Variable Scope The scope of a variable is the block or blocks in which the variable is accessible, i.e. it can be named and used. In PL/SQL, a variable s scope is the block in which it is declared plus all blocks nested within the declaring block. What are the scopes of the two variables declared in this example? v_outer_variable VARCHAR2(20):='GLOBAL VARIABLE'; v_inner_variable VARCHAR2(20):='LOCAL VARIABLE'; DBMS_OUTPUT.PUT_LINE(v_inner_variable); DBMS_OUTPUT.PUT_LINE(v_outer_variable); DBMS_OUTPUT.PUT_LINE(v_outer_variable); 5

Variable Scope Examine the following code. What is the scope of each of the variables? v_father_name VARCHAR2(20):='Patrick'; v_date_of_birth DATE:='20-Apr-1972'; v_child_name VARCHAR2(20):='Mike'; DBMS_OUTPUT.PUT_LINE('Father''s Name: ' v_father_name); DBMS_OUTPUT.PUT_LINE('Date of Birth: ' v_date_of_birth); DBMS_OUTPUT.PUT_LINE('Child''s Name: ' v_child_name); DBMS_OUTPUT.PUT_LINE('Date of Birth: ' v_date_of_birth); 6

Local and Global Variables Variables declared in a PL/SQL block are considered local to that block and global to all its subblocks. v_outer_variable is local to the outer block but global to the inner block. When you access this variable in the inner block, PL/SQL first looks for a local variable in the inner block with that name. If there are no similarly named variables, PL/SQL looks for the variable in the outer block. v_outer_variable VARCHAR2(20):='GLOBAL VARIABLE'; v_inner_variable VARCHAR2(20):='LOCAL VARIABLE'; DBMS_OUTPUT.PUT_LINE(v_inner_variable); DBMS_OUTPUT.PUT_LINE(v_outer_variable); DBMS_OUTPUT.PUT_LINE(v_outer_variable); 7

Local and Global Variables The v_inner_variable variable is local to the inner block and is not global because the inner block does not have any nested blocks. This variable can be accessed only within the inner block. If PL/SQL does not find the variable declared locally, it looks upward in the declarative section of the parent blocks. PL/SQL does not look downward in the child blocks. v_outer_variable VARCHAR2(20):='GLOBAL VARIABLE'; v_inner_variable VARCHAR2(20):='LOCAL VARIABLE'; DBMS_OUTPUT.PUT_LINE(v_inner_variable); DBMS_OUTPUT.PUT_LINE(v_outer_variable); DBMS_OUTPUT.PUT_LINE(v_outer_variable); 8

Variable Scope The variables v_father_name and v_date_of_birth are declared in the outer block. They are local to the outer block and global to the inner block. Their scope includes both blocks. v_father_name VARCHAR2(20):='Patrick'; v_date_of_birth DATE:='20-Apr-1972'; v_child_name VARCHAR2(20):='Mike';... The variable v_child_name is declared in the inner (nested) block. This variable is accessible only within the nested block and is not accessible in the outer block. 9

Variable Naming You cannot declare two variables with the same name in the same block. However, you can declare variables with the same name in two different blocks (nested blocks). The two items represented by the same name are distinct, and any change in one does not affect the other. 10

Variable Visibility What if the same name is used for two variables, one in each of the blocks? In this example, the variable v_date_of_birth is declared twice. v_father_name VARCHAR2(20):='Patrick'; v_date_of_birth DATE:='20-Apr-1972'; v_child_name VARCHAR2(20):='Mike'; v_date_of_birth DATE:='12-Dec-2002'; DBMS_OUTPUT.PUT_LINE('Date of Birth: ' v_date_of_birth);... Which v_date_of_birth will be referenced in the DBMS_OUTPUT.PUT_LINE statement? 11

Variable Visibility The visibility of a variable is the portion of the program where the variable can be accessed without using a qualifier. What is the visibility of each of the variables? 1 2 v_father_name VARCHAR2(20):='Patrick'; v_date_of_birth DATE:='20-Apr-1972'; v_child_name VARCHAR2(20):='Mike'; v_date_of_birth DATE:='12-Dec-2002'; DBMS_OUTPUT.PUT_LINE('Father''s Name: ' v_father_name); DBMS_OUTPUT.PUT_LINE('Date of Birth: ' v_date_of_birth); DBMS_OUTPUT.PUT_LINE('Child''s Name: ' v_child_name); DBMS_OUTPUT.PUT_LINE('Date of Birth: ' v_date_of_birth); 12

Variable Visibility The v_date_of_birth variable declared in the outer block has scope even in the inner block. This variable is visible in the outer block. However, it is not visible in the inner block because the inner block has a local variable with the same name. The v_father_name variable is visible in the inner and outer blocks. The v_child_name variable is visible only in the inner block. v_father_name VARCHAR2(20):='Patrick'; v_date_of_birth DATE:='20-Apr-1972'; v_child_name VARCHAR2(20):='Mike'; v_date_of_birth DATE:='12-Dec-2002'; What if we want to reference the outer block s v_date_of_birth within the inner block? 13

Qualifying an Identifier A qualifier is a label given to a block. You can use this qualifier to access the variables which have scope but are not visible. In this example, the outer block has the label, <<outer>>. <<outer>> v_father_name VARCHAR2(20):='Patrick'; v_date_of_birth DATE:='20-Apr-1972'; v_child_name VARCHAR2(20):='Mike'; v_date_of_birth DATE:='12-Dec-2002'; Labeling is not limited to the outer block; you can label any block. 14

Qualifying an Identifier Using the outer label to qualify the v_date_of_birth identifier, you can now print father s date of birth in the inner block. <<outer>> v_father_name VARCHAR2(20):='Patrick'; v_date_of_birth DATE:='20-Apr-1972'; v_child_name VARCHAR2(20):='Mike'; v_date_of_birth DATE:='12-Dec-2002'; DBMS_OUTPUT.PUT_LINE('Father''s Name: ' v_father_name); DBMS_OUTPUT.PUT_LINE('Date of Birth: ' outer.v_date_of_birth); DBMS_OUTPUT.PUT_LINE('Child''s Name: ' v_child_name); DBMS_OUTPUT.PUT_LINE('Date of Birth: ' v_date_of_birth); 15

Scope of Exceptions in Nested Blocks An exception can be dealt with by: Handling it ( trapping it ) in the block in which it occurs, or Propagating it to the calling environment Exception raised Is the exception trapped? no Propagate to calling environment yes Handle with exception handler 16

Trapping Exceptions with a Handler Include an EXCEPTION section in your PL/SQL program to trap exceptions. If the exception is raised in the executable section of the block, processing is handled by the corresponding exception handler in the exception section of the same block. If PL/SQL successfully handles the exception, then the exception does not propagate to the enclosing block or to the calling environment. The PL/SQL block terminates successfully. Exception raised Is the exception trapped? no Propagate to calling environment yes Execute statements in the EXCEPTION section Terminate gracefully 17

Handling Exceptions in an Inner Block In this example, an error occurs during the execution of the inner block. The inner block s EXCEPTION section deals with the exception successfully, and PL/SQL considers that this exception is now finished with. The outer block resumes execution as normal.... -- outer block -- inner block... -- exception_name occurs here... EXCEPTION WHEN exception_name THEN -- handled here... -- inner block terminates successfully... -- outer block continues execution 18

Propagating Exceptions to an Outer Block If the exception is raised in the executable section of the inner block and there is no corresponding exception handler, the PL/SQL block terminates with failure and the exception is propagated to an enclosing block. Exception raised Is the exception trapped? no Terminate abruptly Propagate the exception yes Execute statements in the EXCEPTION section Terminate gracefully 19

Propagating Exceptions to an Outer Block In this example, an error occurs during the execution of the inner block. The inner block s EXCEPTION section does not deal with the exception. The inner block terminates unsuccessfully and PL/SQL passes the exception to the outer block.the outer block s EXCEPTION section successfully handles the exception. -- outer block... -- inner block... - exception_name occurs here... -- inner block terminates unsuccessfully... -- Remaining code in outer block s executable... -- section is skipped EXCEPTION WHEN exception_name THEN outer block handles the exception... 20

Propagating Exceptions in a Subblock If a PL/SQL raises an exception and the current block does not have a handler for that exception, the exception propagates to successive enclosing blocks until it finds a handler. When the exception propagates to an enclosing block, the remaining executable actions in that block are bypassed. One advantage of this behavior is that you can enclose statements that require their own exclusive error handling in their own block, while leaving more general exception handling to the enclosing block. If none of these blocks handle the exception, an unhandled exception occurs in the host environment (for example Application Express). 21

Terminology Key terms used in this lesson include: Variable scope Variable visibility Qualifier Exception handling Exception propagating 22

Summary In this lesson, you have learned how to: Understand the scope and visibility of variables Write nested blocks and qualify variables with labels Understand the scope of an exception Describe the effect of exception propagation in nested blocks 23

Try It / Solve It The exercises in this lesson cover the following topics: Understanding the scope and visibility of variables Writing nested blocks and qualifying variables with labels Understanding the scope of an exception Describing the effect of exception propagation in nested blocks 24