Oracle
  Home arrow Oracle arrow Database Interaction with PL/SQL: Explict Cursors in Depth
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: Explict Cursors in Depth
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 24
    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:
      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: 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
     

       

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek