SunQuest
 
       Oracle
  Home arrow Oracle arrow Page 2 - Database Interaction with PL/SQL: Nest...
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Database Interaction with PL/SQL: Nested Blocks in Depth - Labeling the PL/SQL blocks


    (Page 2 of 4 )

    Here, I provide the solution for the problem defined in the previous section, using the concept of labeling the PL/SQL blocks. Consider the following example:

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

      <<child>>
      Declare
        v_empno emp.empno%Type := &empno2;
        v_ename emp.ename%type;
      Begin
        Select ename into v_ename
        From emp Where empno=parent.v_empno;
        dbms_output.put_line('Empno: '|| parent.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;

    The '<<parent>>' label performs the whole magic. That is how we label a block. Even the nested block in the above program got labeled with '<<child>>'. Actually, it is not necessary to label a child block in this case. But, I did label it, just to inform you that you can also label child (nested) blocks in that manner.

    The SELECT statement in the above program is also bit different (in WHERE condition). We are using 'parent.v_empno' to denote that we are trying to access the variable 'v_empno' of '<<parent>>' block. This notation itself makes it much clearer to PL/SQL runtime that it needs to access the variable of the parent block (but not the local variable of child block).

    The most important issue to remember is that this notation (or labeling) works only with parent-child types of blocks but not paralleled blocks within the same parent block. The following example is INVALID as it tries to access the variable of its own adjacent block (but not parent block).

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

      <<child1>>
      Declare
        v_empno emp.empno%Type := &empno2;
        v_ename emp.ename%type;
      Begin
        Select ename into v_ename
        From emp Where empno=parent.v_empno;
        dbms_output.put_line('Empno: '|| parent.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;

      <<child2>>
      Declare
        v_ename emp.ename%type;
      Begin
        Select ename into v_ename
        From emp Where empno=child1.v_empno; --this is invalid
        dbms_output.put_line('Empno: '|| parent.v_empno); --this is valid
        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;

    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





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