Oracle
  Home arrow Oracle arrow Page 3 - Developing Simple PL/SQL Stored Procedures for CRUD Operations
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

Developing Simple PL/SQL Stored Procedures for CRUD Operations
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 32
    2007-02-19


    Table of Contents:
  • Developing Simple PL/SQL Stored Procedures for CRUD Operations
  • Validate information before inserting a row using a PL/SQL stored procedure: code
  • Validate information before inserting a row using a PL/SQL stored procedure: explanation
  • How to update a row in a table using a PL/SQL stored procedure
  • Deleting and retrieving values using PL/SQL stored procedures

  • 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


    Developing Simple PL/SQL Stored Procedures for CRUD Operations - Validate information before inserting a row using a PL/SQL stored procedure: explanation
    ( Page 3 of 5 )

    This section explains the code provided in the previous section.  Let me start with the following:

          Invalid_sal       exception;
     
         Invalid_deptno    exception;

    “Invalid_Sal” and “Invalid_Deptno” are two user-defined exceptions which can be raised within our logic according to our requirements.  The following is the code fragment which checks for the validity of salary:

          if p_sal<100 or p_sal>10000 then
                raise invalid_sal;
          end if;

    In the above code fragment, if the salary is not in between 100 and 10000, it raises the user-defined exception “Invalid_sal.”  Once it is raised, the control (or flow of execution) jumps to the exception section, and gets the following to be executed:

          when invalid_sal then
                raise_application_error(-20001, 'Salary must be in
    between 100 and 10000');

    The above statement returns a user-defined message to the application which called the stored procedure.

    Coming to the department validation, I need to check whether the given department number exists in the “dept” table or not.  If it is not available, I would like to raise an exception and send a message back to the user.  The following is the PL/SQL block which is nested into the main block to handle the same:

          declare
                dummy_var   varchar(10);
          begin
                select 'exists' into dummy_var
                from dept
                where deptno = p_deptno;
          exception
                when no_data_found then
                      raise Invalid_deptno;
                when others then
                      raise_application_error(-20011, sqlerrm);
          end;

    The above checks to see whether the department exists or not.  If it doesn’t exist, the SELECT statement fails.  If it fails, “no_data_found” gets raised, which in turn fires the “invalid_deptno” exception.  If any other error occurs, it simply raises a default error.

    If the “Invalid_deptno” exception is raised, control (or flow of execution) jumps to the following:

          when invalid_deptno then
                raise_application_error(-20001, 'Department doesn''t exist');



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek