SunQuest
 
       Oracle
  Home arrow Oracle arrow Page 2 - Database Interaction with PL/SQL, part...
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, part 2
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 17
    2005-05-31

    Table of Contents:
  • Database Interaction with PL/SQL, part 2
  • UPDATE with RETURNING clause
  • TYPE with RECORD declaration
  • Accessing more than one row in PL/SQL (TYPE with TABLE)

  • 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!

    Database Interaction with PL/SQL, part 2 - UPDATE with RETURNING clause


    (Page 2 of 4 )

    UPDATE in PL/SQL can be enhanced with the RETURING clause to achieve wonderful results. This clause actually eliminates the use of the SELECT statement after an UPDATE to check the back table (or row) and present the updated result to the user. Let me explain this in more detail.

    Let us consider that we need to increase the salary of an employee by 500 and return the updated salary to the user. Generally, in this situation, we would write our program as the following:

    declare

    v_sal emp.sal%type;

    v_empno emp.empno%type := &empno;

    begin

    update emp set sal = sal + 500

    where empno = v_empno;

    select sal into v_sal from emp

    where empno = v_empno;

    dbms_output.put_line ('Salary increased to ' || v_sal);

    end;

    This is a very old and traditional way of writing the program. We can observe from the above program that there will be two round trips to the database. One round trip is for the UPDATE statement, and the other is for the SELECT statement. We can turn those two round trips to the database into one by using the RETURNING clause. The following program illustrates that.

    declare

    v_sal emp.sal%type;

    v_empno emp.empno%type := &empno;

    begin

    update emp set sal = sal + 500

    where empno = v_empno

    RETURNING sal INTO v_sal;

    dbms_output.put_line ('Salary increased to ' || v_sal);

    end;

    I hope the above program is self-explanatory. But, what if I want a larger number of values to be returned? We can still use the same clause, but separated by commas, as shown in the following example.

    Declare

    v_ename emp.ename%type;

    v_sal emp.sal%type;

    v_empno emp.empno%type := &empno;

    begin

    update emp set sal = sal + 500

    where empno = v_empno

    RETURNING ename,sal INTO v_ename, v_sal;

    dbms_output.put_line ('Salary increased to ' || v_sal || ' for ' || v_ename);

    end;

    More Oracle Articles
    More By Jagadish Chatarji


       · I liked the way you express yourself in a clear and concise way. God bless
     

       

    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