PL-SQL
Oracle PL/SQL Stored Procedure & Functions with Examples
In this tutorial, you are going to see the detailed description on how to create and execute the...
A Cursor is a pointer to this context area. Oracle creates context area for processing an SQL statement which contains all information about the statement.
PL/SQL allows the programmer to control the context area through the cursor. A cursor holds the rows returned by the SQL statement. The set of rows the cursor holds is referred as active set. These cursors can also be named so that they can be referred from another place of the code.
In this tutorial you will learn-
The cursor is of two types.
Whenever any DML operations occur in the database, an implicit cursor is created that holds the rows affected, in that particular operation. These cursors cannot be named and, hence they cannot be controlled or referred from another place of the code. We can refer only to the most recent cursor through the cursor attributes.
Programmers are allowed to create named context area to execute their DML operations to get more control over it. The explicit cursor should be defined in the declaration section of the PL/SQL block, and it is created for the 'SELECT' statement that needs to be used in the code.
Below are steps that involved in working with explicit cursors.
Declaring the cursor simply means to create one named context area for the 'SELECT' statement that is defined in the declaration part. The name of this context area is same as the cursor name.
Opening the cursor will instruct the PL/SQL to allocate the memory for this cursor. It will make the cursor ready to fetch the records.
In this process, the 'SELECT' statement is executed and the rows fetched is stored in the allocated memory. These are now called as active sets. Fetching data from the cursor is a record-level activity that means we can access the data in a record-by-record way.
Each fetch statement will fetch one active set and holds the information of that particular record. This statement is same as 'SELECT' statement that fetches the record and assigns to the variable in the 'INTO' clause, but it will not throw any exceptions.
Once all the record is fetched now, we need to close the cursor so that the memory allocated to this context area will be released.
Syntax:
DECLARE CURSOR <cursor_name> IS <SELECT statement^> <cursor_variable declaration> BEGIN OPEN <cursor_name>; FETCH <cursor_name> INTO <cursor_variable>; . . CLOSE <cursor_name>; END;
Both Implicit cursor and the explicit cursor has certain attributes that can be accessed. These attributes give more information about the cursor operations. Below are the different cursor attributes and their usage.
Cursor Attribute | Description |
%FOUND | It returns the Boolean result 'TRUE' if the most recent fetch operation fetched a record successfully, else it will return FALSE. |
%NOTFOUND | This works oppositely to %FOUND it will return 'TRUE' if the most recent fetch operation could not able to fetch any record. |
%ISOPEN | It returns Boolean result 'TRUE' if the given cursor is already opened, else it returns 'FALSE' |
%ROWCOUNT | It returns the numerical value. It gives the actual count of records that got affected by the DML activity. |
Example 1: In this example, we are going to see how to declare, open, fetch and close the explicit cursor.
We will project all the employee's name from emp table using a cursor. We will also use cursor attribute to set the loop to fetch all the record from the cursor.
DECLARE CURSOR guru99_det IS SELECT emp_name FROM emp; lv_emp_name emp.emp_name%type; BEGIN OPEN guru99_det; LOOP FETCH guru99_det INTO lv_emp_name; IF guru99_det%NOTFOUND THEN EXIT; END IF; Dbms_output.put_line(‘Employee Fetched:‘||lv_emp_name); END LOOP; Dbms_output.put_line(‘Total rows fetched is‘||guru99_det%R0WCOUNT); CLOSE guru99_det; END: /
Output
Employee Fetched:BBB Employee Fetched:XXX Employee Fetched:YYY Total rows fetched is 3
Code Explanation:
"FOR LOOP" statement can be used for working with cursors. We can give the cursor name instead of range limit in the FOR loop statement so that the loop will work from the first record of the cursor to the last record of the cursor. The cursor variable, opening of cursor, fetching and closing of the cursor will be done implicitly by the FOR loop.
Syntax:
DECLARE CURSOR <cursor_name> IS <SELECT statement>; BEGIN FOR I IN <cursor_name> LOOP . . END LOOP; END;
Example 1: In this example, we will project all the employee name from emp table using a cursor-FOR loop.
DECLARE CURSOR guru99_det IS SELECT emp_name FROM emp; BEGIN FOR lv_emp_name IN guru99_det LOOP Dbms_output.put_line(‘Employee Fetched:‘||lv_emp_name.emp_name); END LOOP; END; /
Output
Employee Fetched:BBB Employee Fetched:XXX Employee Fetched:YYY
Code Explanation:
Note: In Cursor-FOR loop, cursor attributes cannot be used since opening, fetching and closing of the cursor is done implicitly by FOR loop.
In this tutorial, you are going to see the detailed description on how to create and execute the...
What are Decision-Making Statements? Decision making statements are those who will decide the...
What is BULK COLLECT? BULK COLLECT reduces context switches between SQL and PL/SQL engine and...
What is Trigger in PL/SQL? TRIGGERS are stored programs that are fired by Oracle engine...
What is CASE Statement? A CASE statement is similar to IF-THEN-ELSIF statement that selects one...
What is Identifiers? Identifiers are nothing but a name that is given to a PL/SQL object. The...