SunQuest
 
       Oracle
  Home arrow Oracle arrow Page 2 - Database Interaction with PL/SQL: Sub-...
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: Sub-programs in Depth
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2005-08-16

    Table of Contents:
  • Database Interaction with PL/SQL: Sub-programs in Depth
  • Sub-programs interacting with an Oracle database
  • Procedures with parameters
  • Using %ROWTYPE in parameter declarations
  • Introduction to FUNCTION

  • 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
     
    IBM developerWorks
     
    ADVERTISEMENT

    Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!

    Database Interaction with PL/SQL: Sub-programs in Depth - Sub-programs interacting with an Oracle database


    (Page 2 of 5 )

    My previous article, and even until now, I only focused on general programs using sub-programs. I didn’t interact with database information at all. Now, let us see how to interact with a database by using a simple example:

    declare

                procedure dispEmp as

                            cursor c_emp is

                                        select ename, sal from emp;

                begin

                            for r_emp in c_emp

                            loop

                                        dbms_output.put_line(r_emp.ename || ',' || r_emp.sal);  

                            end loop;

                end;

    BEGIN

                dbms_output.put_line('-----------------');

                dbms_output.put_line('EMPLOYEES');

                dbms_output.put_line('-----------------');

                dispEmp;

    END;

    I hope the above program is self explanatory. I just used the concept of CURSOR (discussed in my previous articles) within the sub-program to interact with the database. And I am executing the sub-program from the main program. Let us further extend the above program by adding one more sub-program as follows:

    declare

                procedure dispEmp as

                            cursor c_emp is

                                        select ename, sal from emp;

                begin

                            for r_emp in c_emp

                            loop

                                        dbms_output.put_line(r_emp.ename || ',' || r_emp.sal);  

                            end loop;

                end;

                procedure dispDept as

                            cursor c_dept is

                                        select deptno,dname from dept;

                begin

                            for r_dept in c_dept

                            loop

                                        dbms_output.put_line(r_dept.deptno || ',' || r_dept.dname);

                            end loop;

                end;

    BEGIN

                dbms_output.put_line('-----------------');

                dbms_output.put_line('EMPLOYEES');

                dbms_output.put_line('-----------------');

                dispEmp;

                dbms_output.put_line('-----------------');

                dbms_output.put_line('DEPARTMENTS');

                dbms_output.put_line('-----------------');

                dispDept;

    END;

    Actually there exists nothing new in the above program. We already covered each and every statement earlier. The only difference is the logic. So, let’s start at BEGIN (capital BEGIN). I displayed a heading ‘EMPLOYEES’ (over-lined and under-lined as well). The next statement is “dispEmp”. The control jumps to ‘dispEmp’. 

    The sub-program ‘dispEmp’ just displays the whole information for employees using CURSOR (discussed in my previous articles). After the completion of sub-program ‘dispEmp’, the control returns back to the next statement of the calling statement at the main program. Here the calling statement is the statement which calls the sub-program (which is ‘dispEmp’ in main program). Again it displays a heading ‘DEPARTMENTS’ (over-lined and under-lined as well). The next statement is ‘dispDept’. Now, the control jumps to ‘dispDept’. The sub-program ‘dispDept’ just displays the whole information for departments using CURSOR.

    I gave this example to help you understand only the flow of control in between the main program and sub-programs.

    More Oracle Articles
    More By Jagadish Chatarji


       · Hello guys, this is my in-depth article on working with sub-programs 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 6 hosted by Hostway