Oracle
  Home arrow Oracle arrow Page 5 - 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 
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

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

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    Developing Simple PL/SQL Stored Procedures for CRUD Operations - Deleting and retrieving values using PL/SQL stored procedures


    (Page 5 of 5 )

    The following is the code which simply deletes a row from the table based on the employee number sent to it:

    create or replace procedure p_emp_delete (p_empno emp.empno%type) as
          Invalid_empno exception;
    begin
          delete from emp
          where empno=p_empno;
          if sql%notfound or sql%rowcount=0 then
                rollback;
                raise Invalid_empno;
          end if;
          commit;
    exception
          when invalid_empno then
                raise_application_error(-20001, 'Employee does not
    exist');
          when others then
                raise_application_error(-20011, sqlerrm);
    end;
    /

    You can understand that the above code is very similar to the “p_emp_update” code given in previous section.  Now we shall proceed with retrieving information.

    There exist several methods for retrieving information available within an Oracle database.  If you would like to retrieve one or very few definite values, then it is advisable to proceed with OUT parameters.  If you would like to retrieve an entire row (or set of rows), it is preferable to work with REF CURSORS.  If you require more information about REF CURSORS, I have already contributed a separate article on REF CURSORS, which you can easily find on this web site.

    Now, in this scenario, I shall simply proceed with retrieving a set of values using OUTPUT parameters.

    create or replace procedure p_emp_details(p_empno emp.empno%type,
    p_ename OUT emp.ename%type, p_sal OUT emp.sal%type, p_deptno OUT
    emp.deptno%type)
    as
    begin
          select ename, sal, deptno
          into p_ename, p_sal, p_deptno
          from emp
          where empno = p_empno;
    exception
          when no_data_found then
                raise_application_error(-20001, 'Employee not
    found');
          when too_many_rows then
                /* this would not happen generally */
                raise_application_error(-20002, 'More employees exist
    with the same number');
          when others then
                raise_application_error(-20003, SQLERRM);
    end;
    /

    In the above stored procedure, you can observe that all the parameters (except p_empno) are OUTPUT parameters.  They are mainly used to push the data OUT of the stored procedure to the calling program.  To execute the above stored procedure, you may have to use a separate script (or PL/SQL block) as follows:

    declare
          s     emp.sal%type;
          en    emp.ename%type;
          d     emp.deptno%type;
    begin
          p_emp_details(7369, en,s,d);
          dbms_output.put_line('name of employee: ' || en);
          dbms_output.put_line('Salary: ' || s);
          dbms_output.put_line('Deptno: '||d);
    end;
    /

    The above script simply declares a few variables to hold the values returned by the stored procedure.

    My next article will look into the aspects of retrieving multiple rows using REF CURSOR, packages, helper routines etc.  My further upcoming articles will deal with DAL (Data Access Layer) using ODP.NET together with ASP.NET to access these stored procedures (for www.aspfree.com).  I hope you enjoyed this article and any comments, suggestions, feedback, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 5 hosted by Hostway