Oracle
  Home arrow Oracle arrow Page 3 - 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 
Moblin 
JMSL Numerical Library 
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


    Database Interaction with PL/SQL, part 2 - TYPE with RECORD declaration


    (Page 3 of 4 )

    Until now, we have been working either with %TYPE or %ROWTYPE. This means we are working with either one value or one complete record. How do we create our own data type, with our own specified number of values to hold? This is where TYPE and RECORD come in. Those who are familiar with the C language can consider this to be a structure declaration. Let me explain this to you in detail.

    Let us consider a table of about 20 columns.  I always need to work with only seven of those columns. If I use %ROWTYPE, I get all 20 values unnecessarily. At the same time, my program will be bit clumsy if I use seven %TYPE declarations. A better way to solve this solution is by defining my own data type, which can hold seven values. Then I declare the variables of my new data type and work with them. The following example illustrates this.

    declare

    TYPE t_emprec IS RECORD

    (

    name emp.ename%type,

    salary emp.sal%type,

    job emp.job%type

    );

    v_emp t_emprec;

    v_empno emp.empno%type := &empno;

    begin

    select ename,sal,job into v_emp

    from emp where empno = v_empno;

    dbms_output.put_line ('Name : ' || v_emp.name);

    dbms_output.put_line ('Salary : ' || v_emp.salary);

    dbms_output.put_line ('Job : ' || v_emp.job);

    end;

    Let us examine the above program part by part. First, consider the following:

    TYPE t_emprec IS RECORD

    (

    name emp.ename%type,

    salary emp.sal%type,

    job emp.job%type

    );

    The above defines a new data type named "t_emprec" (just like %ROWTYPE with limited specified fields) which can hold three fields, namely "name," "salary" and "job."

    v_emp t_emprec;

    The above statement declares a variable "v_emp" based on the datatype "t_emprec." This means that "v_emp" internally contains the fields "name," "salary" and "job."

    select ename,sal,job into v_emp

    from emp where empno = v_empno;

    The above statement places the values of "ename," "sal" and "job" into fields "name," "salary" and "job" of the variable "v_emp" respectively.

    dbms_output.put_line ('Name : ' || v_emp.name);

    dbms_output.put_line ('Salary : ' || v_emp.salary);

    dbms_output.put_line ('Job : ' || v_emp.job);

    And the above displays all the values available fields of the variable "v_emp."

    More Oracle Articles
    More By Jagadish Chatarji


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

       

    ORACLE ARTICLES

    - 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...
    - Sub-templates and More with Oracle HTML DB





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway