Oracle
  Home arrow Oracle arrow Database Interaction with PL/SQL: Introduction to 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: Introduction to Sub-programs
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 18
    2005-08-09


    Table of Contents:
  • Database Interaction with PL/SQL: Introduction to Sub-programs
  • Coding our first sub-program
  • Variables and scope in sub-programs
  • Variable scope bit in depth
  • More 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: Introduction to Sub-programs
    ( Page 1 of 5 )

    This is part 12 of a series of articles focusing on database interactions with Oracle PL/SQL. In my previous article, I looked at several examples of explicit cursors. I also introduced the concept of cursors with parameters. In this article we will look into sub-programs. Mainly we will concentrate on procedures and discuss some issues of variable scoping with respect to sub-programs.

    Actually, I need to discuss the concept of REF CURSOR at this moment, but the REF CURSOR is mainly used in sub-programs (or even packages). So, I thought to discuss the sub-programs and packages before going to REF CURSOR.

    Please note that all the examples in this series have been tested only with Oracle 10g. I didn’t really test them with all the previous versions of Oracle. I suggest you to refer the documentation of respective version you are using, if any of the programs failed to execute.

    Introduction to sub-programs

    Almost every programming language supports sub-programs. They are a part of structured programming. A sub-program is a part of a main program separated completely from the main program. We can consider it as separate block of statements (with its own declarations and programming statements), but still under the control of the main program.

    The above explanation may match a bit with anonymous blocks (or nested blocks) of PL/SQL programs (discussed in my previous set of articles), but sub-programs are totally different from nested blocks.  Nested blocks would definitely execute within the main block. But a sub-program need not do so. How is this possible?

    It's really quite simple. As I explained earlier, it is a separate block, separately named, and the main issue is that it is separately called by the main block. The main block calls the sub-program by its name to execute its set of statements. This may be a part of the conditions as well.  That means the main program may or may not call sub-program based on certain conditions.

    Why is this concept necessary? Repeatable tasks (or the tasks which are being executed quite frequently) are generally separated as sub-programs to facilitate the user to use them as many times as possible.  This also improves the clarity (or readability) of the main program, which is being divided into several meaningful tasks, where each task (sub-program) may have its own set of programming statements (including local declarations).

    This concept is mainly taken from the initiative of modularization. It is the process by which you break up large blocks of code into smaller pieces called modules (or sub-programs) which can be called by other modules. With the concept of modularization, our code becomes more reusable, more manageable, more readable and finally more reliable.  This is not only for sub-programs; it is being implemented in the form of packages (to be dealt with in my upcoming articles) as well.

    There are mainly two kinds of sub-programs: procedures and functions.  Both are very similar in functionality and syntactical issues. The one difference is that a function returns a value, but a procedure doesn’t.  We will look into the concept of functions in-depth later.



     
     
    >>> 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