Oracle
  Home arrow Oracle arrow Page 2 - Database Interaction with PL/SQL: Pre-...
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 
Moblin 
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: Pre-defined Exceptions
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 16
    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:
      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: 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


       · Hello guys, this is my article on working with pre-defined exceptions in PL/SQL. You...
     

       

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