Oracle
  Home arrow Oracle arrow Database Interaction with PL/SQL: Nest...
The Best Selling PC Migration Utility.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ORACLE

Database Interaction with PL/SQL: Nested Blocks in Depth
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2005-07-12

    Table of Contents:
  • Database Interaction with PL/SQL: Nested Blocks in Depth
  • Labeling the PL/SQL blocks
  • EXCEPTION handling in both parent and nested PL/SQL blocks
  • SQLCODE and SQLERRM

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Database Interaction with PL/SQL: Nested Blocks in Depth
    (Page 1 of 4 )

    This is part eight of a series of articles focusing on database interactions with Oracle PL/SQL. In my previous article, I gave an introduction to user defined exceptions and nested blocks in PL/SQL. In this article, we will look into handling more than one exception and different tips on using nested blocks.

    Please note that all the examples in this series have been tested only with Oracle 10g. I didn't really test them with all the previous versions of Oracle. I suggest you to refer the documentation for the version you are using, if any of the programs failed to execute.

    The problem of using Nested blocks

    The seventh part of my series (previous article) introduced nested blocks in PL/SQL. Let us look into another frequent problem when using nested blocks in PL/SQL. Let us consider the following example:

    Declare
      v_empno emp.empno%Type := &empno1;
    Begin

      Declare
        v_empno emp.empno%Type := &empno2;
        v_ename emp.ename%type;
      Begin
        Select ename into v_ename
        From emp Where empno=v_empno;
        dbms_output.put_line('Empno: '||v_empno);
        dbms_output.put_line('Name: '||v_ename);
      Exception
        When no_data_found then
          dbms_output.put_line('Employee not found with '||v_empno);
      End;

    End;

    If you carefully observe the above program, there exists a variable called 'v_empno' declared both in parent and nested blocks. Normally, a variable cannot be repeated in declarations. But the above program has those declarations in two different declaration sections which are not related to one another. These types of scenarios are acceptable in nested blocks.

    If you execute the above program, it accepts two values (employee numbers) from the user. The first employee number gets stored in 'v_empno' of the parent block and the second in 'v_empno' of the second block. Now the interesting issue is the situation related to the SELECT statement. Don't forget that all parent block variables are accessible in child (nested) blocks. This means that the nested block can logically have the access for three variables namely 'v_ename', 'v_empno' (of nested block) and again 'v_empno' (of parent block). Now the SELECT statement considers which variable (is it the 'v_empno' of the nested block or the 'v_empno' of the parent block?) in its WHERE condition!

    Any block always gives first preference to its own local variables. If it could not find the respective variable in its own block, it tries to search in its parent block. If the parent block defines that variable, it directly accesses it. If the parent block does not define that variable, it would be an error. So, in this case the variable in the nested block overrides the variable in the parent block. Finally, the SELECT statement in the above program always considers the second employee number (which gets stored in 'v_empno' of the nested block).

    After reading the above three paragraphs, you should be convinced that local variables will be given first preference in access. So far, so good. But, what if I wanted to access the same variable ('v_empno') in the parent block within the nested block (or child block)? The next section gives you the solution.

    More Oracle Articles
    More By Jagadish Chatarji


       · Hello guys, this is my article on nested blocks in PL/SQL. You can start discussing...
     

       

    ORACLE ARTICLES

    - Tuning PL/SQL Code
    - Debugging PL/SQL Code
    - Testing PL/SQL Code
    - Working With PL/SQL Code
    - Conditional Compilation for Oracle Database ...
    - Compile-Time Warnings for Oracle DB 10g
    - Compiling PL/SQL Code for an Oracle Database
    - Troubleshooting PL/SQL Code
    - Managing PL/SQL Code
    - Data Manipulation and More for HTML DB Appli...
    - Oracle Database Fundamentals
    - Adding Processes to HTML DB Applications
    - Adding Computations, Processes, and Validati...
    - Sub-templates and More with Oracle HTML DB
    - Focusing on Templates in Oracle HTML DB

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway