Oracle
  Home arrow Oracle arrow Page 3 - 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 
Moblin 
JMSL Numerical Library 
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 Cursors, Implicit Cursors
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 34
    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:
      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


    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


       · Hello guys, this is my article on cursos and implicit cursors in PL/SQL. You can...
       · Hello guys, this is my article on cursos and implicit cursors in PL/SQL. You can...
     

       

    ORACLE ARTICLES

    - 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...
    - Sub-templates and More with Oracle HTML DB





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