Oracle
  Home arrow Oracle arrow Page 2 - Database Interaction with PL/SQL: OBJE...
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 
VeriSign Whitepapers 
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: OBJECT and OBJECT
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2005-06-14

    Table of Contents:
  • Database Interaction with PL/SQL: OBJECT and OBJECT
  • Accessing OBJECT TYPE using PL/SQL
  • Working with column based OBJECTs
  • Accessing column based OBJECTs in PL/SQL

  • 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

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    Database Interaction with PL/SQL: OBJECT and OBJECT - Accessing OBJECT TYPE using PL/SQL


    (Page 2 of 4 )

    We created OBJECT TYPE in the previous section.  Now let us see how we can access the information of OBJECT TYPE using PL/SQL.

    Let us consider the following example.

    declare
        v_experience    t_experience;
        v_ename             varchar2(20) := '&ename';
    begin
        select value(e) into v_experience
        from employees e where ename = v_ename;
        dbms_output.put_line('Company: ' || v_experience.companyname);
        dbms_output.put_line('Years: ' || v_experience.NoOfYears);
    end;

    From the above program, ‘v_experience’ is declared based on the OBJECT TYPE ‘t_experience’.  The most important statement to understand from the above program is the following:

    select value(e) into v_experience
    from employees e where ename = v_ename;

    The table ‘employees’ is aliased as ‘e’ and we are using ‘value’ (in combination with alias) function to return values in the form of an OBJECT (instead of values).  The OBJECT returned by VALUE is placed into ‘v_experience’ variable and I hope the rest is same.

    Another important issue to remember is that, it is not compulsory to return VALUE in the form of object.  We can also use individual variables as the following:

    declare
        v_CompanyName    varchar2(20);
       
    v_NoOfYears      number(4);
        v_ename          varchar2(20) := '&ename';
    begin
        select CompanyName, NoOfYears into v_CompanyName, v_NoOfYears
        from employees e where ename = v_ename;
        dbms_output.put_line('Company: ' || v_CompanyName);
        dbms_output.put_line('Years: ' || v_NoOfYears);
    end;
    /

    The above can also be rewritten using TYPE with RECORD to retrieve OBJECT based TABLE information as following:

    declare
        TYPE t_experience is RECORD
        (
            CompanyName    varchar2(20),
            Years          varchar2(20)
        );
        v_experience    t_experience;
       
    v_ename           varchar2(20) := '&ename';
    begin
        select CompanyName,NoOfYears  into v_experience
        from employees where ename = v_ename;
        dbms_output.put_line('Company: ' || v_experience.CompanyName);
        dbms_output.put_line('Years: ' || v_experience.Years);
    end;
    /

    Till now, all the above programs are working with only single rows of OBJECTs.  What about more number of rows?  We can follow the same approach explained in Part-2 or Part-3 of this series and modify a bit as following:

    declare
        type tbl_experience is table of t_experience;
        v_emptbl          tbl_experience;
        i                       integer;
    begin
        select value(e) bulk collect into v_emptbl from employees e;
        i := v_emptbl.first;
        while i is not null
        loop
            dbms_output.put_line(v_emptbl(i).CompanyName);
            i := v_emptbl.next(i);
        end loop;
    end;
    /

    We must understand that there exists several number of ways to retrieve the OBJECT based TABLE information.  We need to follow certain methods appropriate to the situation rather than confusing ourselves with all of them.

    More Oracle Articles
    More By Jagadish Chatarji


       · Hello guys, this is my article on working with OBJECT and OBJECT TYPE in PL/SQL. You...
     

       

    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