Oracle
  Home arrow Oracle arrow Page 3 - Database Interaction with PL/SQL, Introduction to Cursors, Implicit Cursors
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 Cursors, Implicit Cursors
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 37
    2005-07-19


    Table of Contents:
  • Database Interaction with PL/SQL, Introduction to Cursors, Implicit Cursors
  • Introduction to Cursors
  • The powerful SQL cursor
  • %NOTFOUND, %ROWCOUNT and %ISOPEN attributes

  • 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 Cursors, Implicit Cursors - The powerful SQL cursor
    ( Page 3 of 4 )

    First of all, don't confuse yourself. SQL is a great RDBMS query language, no doubt about it. But I am not talking about the "SQL language." Instead, I am talking about a pre-defined IMPLICIT cursor named "SQL cursor."

    When you execute any DML statement within PL/SQL, we can know its status of execution through this "SQL" implicit cursor. Oracle opens the SQL cursor automatically (when DML starts its execution) and closes it (when the DML statement finishes its execution). We can invoke its attributes at any time in PL/SQL, when working with DML statements.

    Let us start with a simple example.

    Declare
      v_empno emp.empno%type := &empno;
      v_sal emp.sal%Type := &sal;
    Begin
      Update emp set sal = v_sal
        Where empno=v_empno;
      dbms_output.put_line('Salary got Succesfully updated.');
    End;

    I don't think that I need to explain much about the above program. It just updates the salary for the given employee number. But just consider what would happen if I provided an invalid employee number. Will it raise a NO_DATA_FOUND exception? No. Not at all. It will simply gives the message 'Salary got successfully updated', even though if I gave a wrong employee number. Why?

    NO_DATA_FOUND and TOO_MANY_ROWS work only with SELECT..INTO statements, not with any other DML statements. In the above case, it doesn't even raise a proper exception to handle and show to the user that the employee number is invalid.

    In these types of situations, the SQL cursor comes to the rescue. We can always know the status of a DML statement with this SQL cursor very efficiently. ONE SHOULD UNDERSTAND THAT THE SQL CURSOR ALWAYS REFERS TO THE MOST RECENTLY EXECUTED DML STATEMENT. That statement is very important to follow. Now, let us modify the above program to a meaningful program.

    Declare
      v_empno emp.empno%type := &empno;
      v_sal emp.sal%Type := &sal;
    Begin
      Update emp set sal = v_sal
        Where empno=v_empno;
      if SQL%found then
        dbms_output.put_line('Salary got Succesfully updated.');
      else
        dbms_output.put_line('No employee found.');
      end if;
    End;

    The only difference in the above program is the following statement.

    if SQL%found then
      dbms_output.put_line('Salary got Succesfully updated.');
    else
      dbms_output.put_line('No employee found.');
    end if;

    Within 'SQL%found', SQL refers to the SQL cursor and '%found' is the cursor attribute we are using with the SQL cursor. Finally, 'SQL%found' returns 'true' if the most recent DML statement (in this case UPDATE) gets successfully executed. And I hope you can understand the rest.



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