Oracle
  Home arrow Oracle arrow Page 2 - Database Interaction with PL/SQL: Pre-defined Exceptions
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
ORACLE

Database Interaction with PL/SQL: Pre-defined Exceptions
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 18
    2005-06-28


    Table of Contents:
  • Database Interaction with PL/SQL: Pre-defined Exceptions
  • No Data Found, Too Many Rows
  • Dup Val on Index, Zero Divide, Invalid Number, Value Error
  • A Word About OTHERS Exception

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Database Interaction with PL/SQL: Pre-defined Exceptions - No Data Found, Too Many Rows
    ( Page 2 of 4 )

    NO_DATA_FOUND

    NO_DATA_FOUND exception is raised when SELECT…INTO statement could not find any row (or value) to fetch into variables.

    Let us consider the following example:

    Declare
      v_empno emp.empno%Type := &empno;
      v_ename emp.ename%Type;
    Begin
      Select ename into v_ename From emp 
      where empno=v_empno;
      dbms_output.put_line('Name: '||v_ename);
    Exception
      when no_data_found then
        dbms_output.put_line('Employee not found');
    End;

    From the above program, we declare two variables 'v_empno' and 'v_ename'. We accept input from the user just by placing '&empno'. All this happens within declaration section. Within the body (between 'begin' and 'end'), we fetch the 'ename' and place it in 'v_ename' from the table 'emp', based on the 'where' condition framed from the user input. To display the value available in 'v_ename', we use DBMS_OUTPUT.PUT_LINE statement. If employee number could not be found, it will raise an exception where we need to handle it within the EXCEPTION section before 'END' statement.

    The exception section is used to handle any type of exception. So, we need to specify the exception (which is to be handled) using WHEN statement within the exception section. Using DBMS_OUTPUT.PUT_LINE statement we can display the error 'Employee not found' on to the screen.

    The flow of execution of program will be something like the following (in brief):

    • Declares the variables and accepts input from the user
    • Try to execute the SELECT statement and fetch 'ename' into 'v_ename'
    • If SELECT statement gets successfully executed, it displays the employee name (next line of SELECT) and directly jumps to END (skipping the exception section).
    • If SELECT statement could not retrieve any row (in this case it is 'ename'), it immediately jumps to exception section (skipping the DBMS_OUTPUT statement next to SELECT statement) and checks whether the exception (or error) is NO_DATA_FOUND or not. If so, displays the message 'Employee not found'.

    TOO_MANY_ROWS

    Now that we understood about NO_DATA_FOUND, we shall look into TOO_MANY_ROWS. We all know very well that SELECT..INTO statement will not be able to retrieve more than one row. So, when a SELECT ….INTO statement in a PL/SQL blocks tries to return more than one row, TOO_MANY_ROWS exception gets raised. Let us consider the following program.

    Declare
      v_emp emp%rowtype;
      v_Sal emp.sal%Type := &sal;
    Begin
      Select * into v_emp From emp
      where sal=v_sal;
      dbms_output.put_line('Name:'||v_emp.ename);
    Exception
      When Too_Many_rows then
        dbms_output.put_line('More than one employee having same salary');
    End ;

    The above program is very similar to the first program, and the flow of execution of program will be something like the following (in brief):

    • Declares the variables and accepts input from the user
    • Try to execute the SELECT statement and fetch entire row from 'emp' into 'v_emp'
    • If SELECT statement gets successfully executed with only single row, it displays the employee name (next line of SELECT) and directly jumps to END (skipping the exception section).
    • If SELECT statement is trying to get more than one row (as there exists a possibility of having more than one employee with the same salary given), it immediately jumps to exception section (skipping the DBMS_OUTPUT statement next to SELECT statement) and checks whether the exception (or error) is TOO_MANY_ROWS or not. If so, displays the message 'More than one employee having same salary'.



     
     
    >>> More Oracle Articles          >>> More By Jagadish Chatarji
     

       

    ORACLE ARTICLES

    - Oracle's Turn to Play in the Sun
    - Implementing and Using Oracle`s Restore Poin...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek