Oracle
  Home arrow Oracle arrow Database Interaction with PL/SQL: Expl...
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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: Explict Cursors in Depth
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 21
    2005-08-02

    Table of Contents:
  • Database Interaction with PL/SQL: Explict Cursors in Depth
  • Working with more than one cursor
  • How the program works
  • Further approaches to the above program
  • Cursors with parameters – a complicated example
  • Can we use JOINS in cursors?

  • 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

    Virtual Tradeshows by Ziff Davis Enterprise - A Unique Opportunity to Connect with IT Experts, Access Information, and Gain Insight on today's Technology

    Database Interaction with PL/SQL: Explict Cursors in Depth
    (Page 1 of 6 )

    This is part 11 of a series of articles focusing on database interactions with Oracle PL/SQL. In my previous article, I introduced the concept of the explicit cursor and looked at several approaches for using the explicit cursors. In this article, we shall delve more deeply into explicit cursors.

    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.

    A different example

    In my previous article, I gave some examples of explicit cursors. But you can also achieve the same output (of that example) using only SQL statements. Now we shall see a different example using the same concept of explicit cursor.  Let us consider the following example:

     

    declare

      cursor c_dept is

            select deptno,dname from dept;

      r_dept      c_dept%rowtype;

      v_sumsal    number(5);

      v_maxsal    emp.sal%type;

      v_count           number(2);

    begin

      open c_dept;

      loop

            fetch c_dept into r_dept;

            exit when c_dept%notfound;

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

            dbms_output.put_line(r_dept.dname);

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

            select sum(sal) into v_sumsal from emp

                  where deptno = r_dept.deptno;

            select max(sal) into v_maxsal from emp

                  where deptno = r_dept.deptno;

            select count(sal) into v_count from emp

                  where deptno = r_dept.deptno;

            dbms_output.put_line('No. of employees:' || v_count);

            dbms_output.put_line('Highest Salary:' || v_maxsal);

            dbms_output.put_line('Total Salaries:' || v_sumsal);

      end loop;

      close c_dept;

    end;

    The above program iterates through each of the departments and finally displays the output as something like the following:

    --------------
    ACCOUNTING
    --------------
    No. of employees:3
    Highest Salary:5000
    Total Salaries:8750
    --------------
    RESEARCH
    --------------
    No. of employees:5
    Highest Salary:3000
    Total Salaries:10875

    The above output looks somewhat different from the traditional SQL output. We can control the lines of output in our own way. The output need not be in the form of rows divided into columns. We can display anything we like through PL/SQL in a controlled manner. I hope the above program is very simple. I am using three SELECT…INTO statements based on the current department number retrieved through cursor. You can further simplify the same above program as follows (with the use of FOR loop):

    declare

      cursor c_dept is

            select deptno,dname from dept;

      v_sumsal    number(5);

      v_maxsal    emp.sal%type;

      v_count           number(2);

    begin

      for r_dept in c_dept;

      loop

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

            dbms_output.put_line(r_dept.dname);

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

            select sum(sal), max(sal), count(sal)

                  into v_sumsal,v_maxsal,v_count from emp

                  where deptno = r_dept.deptno;

            dbms_output.put_line('No. of employees:' || v_count);

            dbms_output.put_line('Highest Salary:' || v_maxsal);

            dbms_output.put_line('Total Salaries:' || v_sumsal);

      end loop;

    end;

    More Oracle Articles
    More By Jagadish Chatarji


       · Hello guys, this is my in-depth article on working with explicit cursors in PL/SQL....
       · Thank you very much for your in depth look at cursors. It helps me see what else can...
     

       

    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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway