Oracle
  Home arrow Oracle arrow Page 3 - Database Interaction with PL/SQL: User...
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: User defined Exceptions, Nested Blocks
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 24
    2005-07-05

    Table of Contents:
  • Database Interaction with PL/SQL: User defined Exceptions, Nested Blocks
  • A frequently faced problem in exception handling
  • The solution
  • The most efficient solution
  • Nested blocks with local variable declarations

  • 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: User defined Exceptions, Nested Blocks - The solution


    (Page 3 of 5 )

    The following program solves the problem explained in the previous section:

    Declare
      v_SearchingEmpno emp.empno%type;
      v_empno1 emp.empno%Type := &empno1;
      v_ename emp.ename%Type;
      v_empno2 emp.empno%Type := &empno2;
    Begin
      v_SearchingEmpno := v_empno1;
      Select ename into v_ename
      From emp Where empno=v_empno1;
      dbms_output.put_line('First Employee Name: '||v_ename);

      v_SearchingEmpno := v_empno2;
      Select ename into v_ename
      From emp Where empno=v_empno2;
      dbms_output.put_line('Second Employee Name: '||v_ename);

    Exception
      When no_data_found then
        dbms_output.put_line('Employee not found with '||v_SearchingEmpno);
    End

    The magic is in using a temporary variable (v_SearchingEmpno) to hold the employee number, which is being fetched through the SELECT statement.  I stored ‘v_empno1’ into ‘v_SearchingEmpno’ before it is  searched for (or fetched).  If the first SELECT fails, the exception handler displays the value available in ‘v_SearchingEmpno’ (which is nothing but ‘v_empno1’).  The same thing happens with ‘v_empno2’ as well.

    But still there is a minor problem. Even though the above program gives the details of the invalid employee number to the user, it doesn't give you both! Which means, if I provide wrong employee numbers for both 'v_empno1' and 'v_empno2', only one gets reported to the user.

    And another problem is that, if I provide an invalid value to the first employee number and a valid value to the second employee number, we would never get the details of the second employee (even though the second employee number is a valid one). The next section addresses these issues using nested blocks in PL/SQL (a new concept!).

    More Oracle Articles
    More By Jagadish Chatarji


       · Hello guys, this is my article on working with user-defined excptions (of course ,...
     

       

    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 3 hosted by Hostway