Oracle
  Home arrow Oracle arrow Page 4 - 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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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 / 19
    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


    Database Interaction with PL/SQL, User-defined Packages - TYPE declarations in package specification


    (Page 4 of 5 )

    A package specification can not only contain sub-programs, it can even contain TYPE declarations as well.  What is the use of declaring TYPE declarations at the level of package specification?  Why don’t we declare the same within the sub-programs (as we usually do)?

    If we declare any TYPE declaration within the package specification, it will be available to the public.  The word ‘public’ has several meanings here.  Any data structure declared in the specification of a package is a global, public data structure. This means that any program outside of the package can access the data structure. Let us see some of the issues of “public” declarations within package specification.

    • A public declaration is available to every sub-program within the same package.
    • It also acts like a stored data type (indirectly).
    • Other stored procedures (or even stored functions) can use the same TYPE any number of times.
    • PL/SQL programs can also use those types declared as public.
    • Apart from all of the above, even external sources can identify these public types and follow accordingly.

    This makes life easier in almost all aspects without remembering and re-declaring the same TYPE again and again.  You can also create a package of constants which are used throughout all of your programs. Then all developers will reference the packaged constants instead of hardcoding the values into their programs. It also proves the concept of single definition at single location, reused several  times at several locations.

    Let’s see an example for the above theory:

    create or replace package SamplePkg as
        TYPE t_emprec is RECORD
        (
            name    emp.ename%TYPE,
            salary  emp.sal%type
         );
         procedure dispEmp;
         procedure dispEmp(p_deptno dept.deptno%type);
    end SamplePkg;
    /

    create or replace package body SamplePkg as
        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;

         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;

    end SamplePkg;
    /

    My next section would help you to understand the above package.

    More Oracle Articles
    More By Jagadish Chatarji


       · 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

    - 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
    Stay green...Green IT