SunQuest
 
       Oracle
  Home arrow Oracle arrow Page 2 - Developing Simple PL/SQL Stored Proced...
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 
IBM developerWorks
 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

Developing Simple PL/SQL Stored Procedures for CRUD Operations
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 24
    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:
      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

    Developing Simple PL/SQL Stored Procedures for CRUD Operations - Validate information before inserting a row using a PL/SQL stored procedure: code


    (Page 2 of 5 )

    In the previous stored procedure, we didn’t do any validations for the information passed to it.  But validating data is the most important priority for any type of solution.

    Validation of data can be achieved using several methods (at the database level).  The simplest one involves using constraints.  But constraints may not be suitable if we have any complex validations.  The better option is to use database triggers.  Database triggers are very similar to stored procedures, but they are automatically executed when the respective DML statement is issued on a table.  We can use database triggers to validate the information and to perform a few calculations as well. 

    As we are dealing with stored procedures, I would like to deal with validations (or even calculations) using stored procedures.  Let us modify the previous stored procedure to handle a few validations as follows:

    create or replace procedure p_emp_insert (p_empno emp.empno%type,
    p_ename emp.ename%type, p_sal emp.sal%type, p_deptno emp.deptno%
    type) as
          Invalid_sal exception;
          Invalid_deptno    exception;
    begin
          if p_sal<100 or p_sal>10000 then
                raise invalid_sal;
          end if;
          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;
          insert into emp
          (
                empno,
                ename,
                sal,
                deptno
          )
          values
          (
                p_empno,
                p_ename,
                p_sal,
                p_deptno
          );
          Commit;
    exception
          when invalid_sal then
                raise_application_error(-20001, 'Salary must be in
    between 100 and 10000');
          when invalid_deptno then
                raise_application_error(-20001, 'Department doesn't
    exist');
          when dup_val_on_index then
                raise_application_error(-20001, 'Employee already
    exists');
          when others then
                raise_application_error(-20011, sqlerrm);
    end;
    /

    The next section will give you a complete explanation for the above stored procedure.

    More Oracle Articles
    More By Jagadish Chatarji


       · Hello guys. This is my contribution, exclusively for application developers. This...
       · Really this really very nice the way it is.some more add programs related to stored...
     

       

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