Cursor
Cursor
● Declare
● Open
● Fetch
● Close
Steps for creating an Explicit Cursor
In case of implicit cursors oracle server performs all
these steps automatically for you.
● Unless the complete cycle of declaring, opening,
fetching and closing has been performed, you can’t
use a cursor.
Declare: How To Declare a Database Cursor?
Declaring a cursor means initializing a cursor into
memory. You define explicit cursor in declaration
section of your PL/SQL block and associate it with
the SELECT statement.
● Syntax
– CURSOR cursor_name IS select_statement;
Open: How to Open a Database Cursor?
– OPEN cursor_name;
Fetch: How to retrieve data from cursor?
The process of retrieving the data from the cursor is
called fetching. Once the cursor is declared and
opened then you can retrieve the data from it. Let’s
see how.
● Syntax
DECLARE
CURSOR cursor_name IS select_statement;
BEGIN
OPEN cursor_name;
FETCH cursor_name INTO PL/SQL variable [PL/SQL record];
CLOSE cursor_name;
END;
Cursor Attributes
Oracle provides four attributes which work in correlation with
cursors. These attributes are:
● %FOUND
● %NOTFOUND
● %ISOPEN
● %ROWCOUNT
%NotFound
%RowCount
%RowCount
● You can use a parameter and then pass different values to the
WHERE clause each time a cursor is opened instead of
hardcoding a value into the WHERE clause of a query to select
particular information.
● Avoids scoping problems