Ticker

6/recent/ticker-posts

PL/SQL - Cursor Attributes in PL/SQL


Cursor Attributes in PL/SQL

Cursors have four attributes, as shown in table 4.2: 
Table 4.2: Cursor Attributes 

Name Description
%FOUND Returns TRUE if the record was fetched successfully, FALSE otherwise.
%NOTFOUND Returns TRUE if the record was not fetched successfully, FALSE otherwise.
%ROWCOUNT Returns the number of records fetched from the cursor at that point in time.
%ISOPEN Returns TRUE if the cursor is open, FALSE otherwise.

Below is an example of how you might use the %NOTFOUND attribute.

CREATE OR REPLACE Function FindCourse
   ( name_in IN varchar2 )
   RETURN number
IS
   cnumber number;

   CURSOR c1
   IS
     SELECT course_number
     FROM courses_tbl
     WHERE course_name = name_in;

BEGIN

   open c1;
   fetch c1 into cnumber;

   if c1%notfound then
      cnumber := 9999;
   end if;

   close c1;

RETURN cnumber;

END;