Unit-4 Database Cursors
Unit-4 Database Cursors
PL/SQL Cursor
When an SQL statement is processed, Oracle creates a memory area known as context area.
A cursor is a pointer to this context area. It contains all information needed for processing the
statement. In PL/SQL, the context area is controlled by Cursor.
A cursor contains information on a select statement and the rows of data accessed by it.
A cursor is used to referred to a program to fetch and process the rows returned by the SQL
statement, one at a time. There are two types of cursors:
o Implicit Cursors
o Explicit Cursors
Orcale provides some attributes known as Implicit cursor's attributes to check the status
of DML operations. Some of them are: %FOUND, %NOTFOUND, %ROWCOUNT and
%ISOPEN.
For example: When you execute the SQL statements like INSERT, UPDATE, DELETE then the
cursor attributes tell whether any rows are affected and how many have been affected. If you
run a SELECT INTO statement in PL/SQL block, the implicit cursor attribute can be used to find
out whether any row has been returned by the SELECT statement. It will return an error if
there no data is selected.
The following table specifies the status of the cursor with each of its attribute.
Attribute Description
%FOUND Its return value is TRUE if DML statements like INSERT, DELETE and UPDATE
affect at least one row or more rows or a SELECT INTO statement returned
one or more rows. Otherwise it returns FALSE.
%NOTFOUND Its return value is TRUE if DML statements like INSERT, DELETE and UPDATE
affect no row, or a SELECT INTO statement return no rows. Otherwise it
returns FALSE. It is a just opposite of %FOUND.
%ISOPEN It always returns FALSE for implicit cursors, because the SQL cursor is
automatically closed after executing its associated SQL statements.
%ROWCOUNT It returns the number of rows affected by DML statements like INSERT,
DELETE, and UPDATE or returned by a SELECT INTO statement.
ID NAME SALARY
1 Ram 20000
2 Shyam 22000
3 Hari 24000
4 Sita 26000
5 Gita 28000
Let's execute the following program to update the table and increase salary of each
customer by 5000. Here, SQL%ROWCOUNT attribute is used to determine the number of
rows affected:
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 5000;
IF sql%notfound THEN
dbms_output.put_line('no customers updated');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers updated ');
END IF;
END;
/
Output:
5 customers updated
PL/SQL procedure successfully completed.
Output:
Statement processed.
101 Ram 25000
102 Shyam 55000
103 Gita 34000
A nice feature of the cursor FOR LOOP statement is that it allows you to fetch every row from
a cursor without manually managing the execution cycle i.e., OPEN, FETCH, and CLOSE.
The cursor FOR LOOP implicitly creates its loop index as a record variable with the row type
in which the cursor returns and then opens the cursor.
In each loop iteration, the cursor FOR LOOP statement fetches a row from the result set into
its loop index. If there is no row to fetch, the cursor FOR LOOP closes the cursor.
The following example declares an explicit cursor and uses it in the cursor FOR
LOOP statement.
Output:
Statement processed.
Eid: 101 Name: Ram
Eid: 102 Name: Shyam
Eid: 103 Name: Gita
Output:
Statement processed.
Eid: 103 Name: Gita
Eid: 102 Name: Shyam
Eid: 101 Name: Ram
Output:
Statement processed.
Eid: 102 Name: Shyam
Output:
Statement processed.
Name: Ram Salary: 25000
Name: Gita Salary: 34000
Output:
Statement processed.
Eid: 102 Name: Shyam
Nested cursor can be used to select data from multiple tables that are in a relationship with
each other. We can also use value accessed by outer cursor in inner cursor.
Employee Table
Department Table
Output:
Statement processed.
Department: IT
Shyam 55000
Department: Finance
Ram 25000
Gita 34000