SunQuest
 
       Oracle
  Home arrow Oracle arrow Page 4 - Database Interaction with PL/SQL: Intr...
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: Introduction to Sub-programs
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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: Introduction to Sub-programs - Variable scope bit in depth


    (Page 4 of 5 )

    Before going into bit depth, let us consider the following program.

    declare

                a          number := 300;

                b          number := 400;

               

                procedure displaySum as

                            a          number := 10;

                            b          number := 20;

                begin

                            dbms_output.put_line('Sum of numbers :' || (a+b+a+b) );

                end;

               

    BEGIN

                dbms_output.put_line('Sum is ' || (a+b) );

                displaySum;

    END;

    If you carefully observe the above program, we have the same variables declared in both the main program and the sub-program. If you watch the output of the above program, you will observe that the procedure displays the result as 60, which is a contradiction. I would like to have ‘a’ and ‘b’ of the sub-program and ‘a’ and ‘b’ of the main program added. But, it didn’t happen like that.

    That means, the preference of the sub-program in the above program could not access the variables in the main program (as the variables are the same as its own local variables). How do we solve the issue?  This is where the concept of ‘Labeling blocks’ comes in once again. Let us modify the above program.

    <<main>>

    declare

                a          number := 300;

                b          number := 400;

               

                procedure displaySum as

                            a          number := 10;

                            b          number := 20;

                begin

                            dbms_output.put_line('Sum of numbers :' || (a+b+ main.a+ main.b ) );

                end;

               

    BEGIN

                dbms_output.put_line('Sum is ' || (a+b) );

                displaySum;

    END;

    If you clearly observe the above program, you can understand that the main program is labeled as “main” and we are able to access the variables of the main program within the sub-program using the convention of “main.variablename”.  Try to observe carefully the DBMS_OUTPUT statement within the sub-program.

    More Oracle Articles
    More By Jagadish Chatarji


       · Hello guys, this is my article introducting sub-programs in PL/SQL. You can start...
       · Please write an article on REF Cursor explaining it in detail and in what different...
       · Thanks for your interest. You will have them soon...
     

       

    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