Oracle
  Home arrow Oracle arrow Page 4 - Database Interaction with PL/SQL, RECORD and TABLE in Sub-programs
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? 
Google.com  
ORACLE

Database Interaction with PL/SQL, RECORD and TABLE in Sub-programs
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 17
    2005-08-23


    Table of Contents:
  • Database Interaction with PL/SQL, RECORD and TABLE in Sub-programs
  • Mixing PROCEDURE and FUNCTION
  • Using RECORD with sub-programs
  • Returning a RECORD from FUNCTION
  • Working with PL/SQL TABLE and sub-programs

  • 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, RECORD and TABLE in Sub-programs - Returning a RECORD from FUNCTION
    ( Page 4 of 5 )

    In the previous section, we have seen how to deal with RECORD using procedures. Now we shall see the same, using FUNCTION. Instead of passing a RECORD to the FUNCTION, we shall return a RECORD from the FUNCTION back to the calling program. Consider the following program:

    declare

    TYPE t_deptrec IS RECORD

    (

    name dept.dname%type,

    location dept.loc%type

    );

    CURSOR c_emp is

    select ename,deptno from emp;

    r_dept t_deptrec;

    function getDept(p_deptno dept.deptno%type) return t_deptrec is

    r_dept t_deptrec;

    begin

    select dname,loc into r_dept

    from dept where deptno = p_deptno;

    return r_dept;

    end;

    BEGIN

    for r_emp in c_emp

    loop

    r_dept := getDept(r_emp.deptno);

    dbms_output.put_line(r_emp.ename || ',' || r_dept.name || ',' || r_dept.location);

    end loop;

    END;

    Let me explain part by part. Consider the following:

    TYPE t_deptrec IS RECORD

    (

    name dept.dname%type,

    location dept.loc%type

    );

    The above defines a new data type named ‘t_deptrec’ (just like %ROWTYPE with limited specified fields) which can hold only two fields, namely ‘name’ and ‘location’.

    CURSOR c_emp is

    select ename,deptno from emp;

    The above statement defined a cursor ‘c_emp’ based on the SELECT statement provided.

    r_dept t_deptrec;

    The above statement declares a variable ‘r_dept’ based on the datatype ‘t_deptrec’. This means ‘r_dept’ internally contains the fields ‘name’ and ‘location’. All of the above declarations are available at the main program level. So, according to logic, we can use them throughout our program (including sub-programs).

    function getDept(p_deptno dept.deptno%type) return t_deptrec is

    r_dept t_deptrec;

    begin

    select dname,loc into r_dept

    from dept where deptno = p_deptno;

    return r_dept;

    end;

    The above FUNCTION receives only a department number as a parameter and retrieves the department name along with the location. The values retrieved get gathered into the ‘r_dept’ variable of type ‘t_deptrec’ (which is local to ‘getDept’ only). And finally we return ‘r_dept’ (which is a RECORD type of variable) back to the main program.

    for r_emp in c_emp

    loop

    r_dept := getDept(r_emp.deptno);

    dbms_output.put_line(r_emp.ename || ',' || r_dept.name || ',' || r_dept.location);

    end loop;

    The main program uses the above loop to display all employee details along with their department names and locations. In the above loop, you should note that we are assigning the value returned by ‘getDept’ into a variable named ‘r_dept’, which is declared in the main program. This has no relation to ‘r_dept’ in ‘getDept’.



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek