Oracle
  Home arrow Oracle arrow Database Interaction with PL/SQL, part 2
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? 
ORACLE

Database Interaction with PL/SQL, part 2
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 18
    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:
      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


    Database Interaction with PL/SQL, part 2
    ( Page 1 of 4 )

    This article picks up where part one left off. You will learn more about how to use %ROWTYPE and %TYPE, and be introduced to TYPE, RECORD, and TABLE declarations in PL/SQL.

    This is part two of a series of articles focusing on database interactions with Oracle PL/SQL. In the previous article (part one), we covered the basics of retrieving information from the Oracle database using the SELECT..INTO statement as well as two attributes, namely "%TYPE" and "%ROWTYPE." We concluded that article with some DML statements working within the PL/SQL program. In this article, we will examine some tips and tricks for using %ROWTYPE and %TYPE and finally concentrate a bit on TYPE, RECORD and TABLE declarations in PL/SQL.

    Please note that all of the examples in this series have been tested only with Oracle 10g. I didn't really test them with all the previous versions of Oracle. I suggest you to refer the documentation of the version you are using, if any of the programs failed to execute.

    %ROWTYPE with INSERT and UPDATE

    We already learned that %ROWTYPE can be used to declare a variable to hold an entire row of information. But how can we use it within DML statements directly? Let me explain through a very simple example.

    declare

    v_dept dept%rowtype;

    begin

    v_dept.deptno := 60;

    v_dept.dname := 'sample';

    v_dept.loc := 'chicago';

    insert into dept values v_dept;

    end;

    The above program illustrates that an "insert" statement can accept a variable of its own table based row type. In the above example, I declared a variable "v_dept," which can store an entire row based on the structure of "dept" table (dept%rowtype). The first three statements within the body assign new department values. The "insert" statement gets all the column values from the variable "v_dept" and inserts them into "dept" table.

    The above program concentrates only on the "insert" statement with the "%rowtype" variable. But how do we work with the "update" statement using the same type of variable? The following example illustrates that.

    declare

    v_dept dept%rowtype;

    begin

    v_dept.deptno := 60;

    v_dept.dname := 'sample2';

    v_dept.loc := 'dallas';

    update dept set ROW=v_dept where deptno=v_dept.deptno;

    end;


    This program is very similar to the earlier program, except for the "update" statement. If you carefully observe the "update" statement, I used a new keyword, "ROW." That performs the magic. The "ROW" can accept any variable of "%rowtype" belonging to its own table (in this case, it is "dept"). This "ROW" keyword assigns each and every column of dept with the values available in "v_dept." And of course, the rest can be understood easily.

    Note that we will not be able to work with "%rowtype" with the DELETE statement (except in the WHERE condition), as the DELETE statement does not actually insert or modify rows in the respective table.



     
     
    >>> 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 3 Hosted by Hostway
    Stay green...Green IT