Friday 6 January 2017

PL/SQL Implicit Cursor





Oracle uses implicit cursors for its internal processing. Even if we execute a SELECT statement or DML statement Oracle reserves a private SQL area in memory called cursor.
Implicit cursor scope you can get information from cursor by using session attributes until another SELECT statement or DML statement execute.

Implicit Cursor Attributes

Following are implicit cursor attributes,
Cursor AttributeCursor VariableDescription
%ISOPENSQL%ISOPENOracle engine automatically open the cursor
If cursor open return TRUE otherwise return FALSE.
%FOUNDSQL%FOUNDIf SELECT statement return one or more rows or DML statement (INSERT, UPDATE, DELETE) affect one or more rows
If affect return TRUE otherwise return FALSE.
If not execute SELECT or DML statement return NULL.
%NOTFOUNDSQL%NOTFOUNDIf SELECT INTO statement return no rows and fire no_data_found PL/SQL exception before you can check SQL%NOTFOUND.
If not affect the row return TRUE otherwise return FALSE.
%ROWCOUNTSQL%ROWCOUNTReturn the number of rows affected by a SELECT statement or DML statement (insert, update, delete).
If not execute SELECT or DML statement return NULL.

Syntax

cursor_attribute ::=
 { 
  cursor_name | 
  cursor_variable_name | 
  :host_cursor_variable_name
 }
 % {FOUND | ISOPEN | NOTFOUND | ROWCOUNT}
Explanation :
cursor_name : cursor_name identifies the current scope which are previously declared.
cursor_variable_name : cursor variable or parameter identifies the current scope which are previously declared.
host_cursor_variable_name : host_cursor_variable_name must be prefixed with a colon. Host cursor variable datatype must be compatible with the PL/SQL cursor variable.

Implicit Cursor Example

Following one emp_information table:
EMP_NOEMP_NAMEEMP_DEPTEMP_SALARY
1Forbs rossWeb Developer45k
2marks jemsProgram Developer38k
3SaulinProgram Developer34k
4Zenia SrollWeb Developer42k
Now above employee information table update the employee name 'Saulin' department 'Program Developer' update to 'Web Developer'.

Example Code

implicit_cursor.sql
SQL>set serveroutput on
SQL>edit implicit_cursor
BEGIN
 UPDATE emp_information SET emp_dept='Web Developer'
  WHERE emp_name='Saulin';

 IF SQL%FOUND THEN
  dbms_output.put_line('Updated - If Found');
 END IF;

 IF SQL%NOTFOUND THEN
  dbms_output.put_line('NOT Updated - If NOT Found');
 END IF; 

 IF SQL%ROWCOUNT>0 THEN
  dbms_output.put_line(SQL%ROWCOUNT||' Rows Updated');
 ELSE
  dbms_output.put_line('NO Rows Updated Found');
 END;
/

Example Result

SQL>@implicit_cursor
Updated - If Found
1 Rows Updated

PL/SQL procedure successfully operation.

    Choose :
  • OR
  • To comment
No comments:
Write comments