Oracle
  Home arrow Oracle arrow Page 5 - Database Interaction with PL/SQL, User...
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, User-defined Packages
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 17
    2005-09-12

    Table of Contents:
  • Database Interaction with PL/SQL, User-defined Packages
  • Database interaction using a PACKAGE
  • Overloading sub-programs in a PACKAGE
  • TYPE declarations in package specification
  • How the above package works

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Database Interaction with PL/SQL, User-defined Packages - How the above package works


    (Page 5 of 5 )

    I gave a lot of theory and a large example in the previous section.  Let me explain the above package bit by bit.  So, first of all consider the following bit:

    TYPE t_emprec is RECORD
        (
            name    emp.ename%TYPE,
            salary  emp.sal%type
         );

    I declared a RECORD type named ‘t_emprec’ within the package specification.  That means it can be used in every sub-program within that package (the minimum consideration).

     procedure dispEmp;
     procedure dispEmp(p_deptno dept.deptno%type);

    The above two are overloaded sub-programs.  One sub-program has a single parameter and the other does not have any parameters.  That’s the end of the package specification.  Now, let us see about the package body.

    procedure dispEmp as
             cursor c_emp is
                  select ename, sal from emp;
              r_emp t_emprec;
          begin
              open c_emp;
              loop
                  fetch c_emp into r_emp;
                  exit when c_emp%notfound;
                  dbms_output.put_line(r_emp.name || ',' ||   r_emp.salary);          
              end loop;
              close c_emp;
           end;

    The above procedure is very similar to the one given in the third section of the article.  It just displays all employee names and salaries.  The only difference is that it fetches the information into a record based variable of type ‘t_emprec’ which was declared in the package specification.

    procedure dispEmp(p_deptno dept.deptno%type) as
             cursor c_emp is
                 select ename, sal from emp
                 where deptno = p_deptno;
             r_emp t_emprec;
        begin
             open c_emp;
             loop
                  fetch c_emp into r_emp;
                  exit when c_emp%notfound;
                  dbms_output.put_line (r_emp.name || ',' || r_emp.salary);          
              end loop;
              close c_emp;
        end;

    I hope you can understand the above sub-program as it is quite similar except in the case of parameter declaration.

    Other extensions to package development

    One more point to understand is that you can also have some declarations within the package body straight away.  That means you can have the declarations at the top of package body (without having any relation to the sub-programs).  Once you have such declarations, those are called private declarations within the package.  That means you can use all of those declarations in each of the sub-programs available. 

    Such types of package body level variable declarations are generally used to implement security within the package context (hiding it from outside world).

    Even though I didn’t use any functions in any of the above packages, it doesn’t mean that you shouldn’t use them.  You can use them as freely as you wish.  And another great feature is that you can call another sub-program within the same sub-program without specifying the package name.  Of course you are also allowed to call the sub-programs which are outside the package as well.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hello guys, this is my new article on working with packages in PL/SQL. You can...
       · when i try one small example,it gives like thispackage,function,procedure and type...
     

       

    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 1 hosted by Hostway