Notes Chapter 2.3 Lecture 2.3.4 (Cursors)
Notes Chapter 2.3 Lecture 2.3.4 (Cursors)
CHAPTER 2.2
When you work with Oracle database, you work with a complete set of rows returned from
an SELECT statement. However the application in some cases cannot work effectively with
the entire result set, therefore, the database server needs to provide a mechanism for the
application to work with one row or a subset of the result set at a time. As the result, Oracle
created PL/SQL cursor to provide these extensions.
A PL/SQL cursor is a pointer that points to the result set of an SQL query against database
tables.
The following picture describes steps that you need to follow when you work with a PL/SQL
cursor:
Oracle has dedicated memory locations for executing SQL statements and then it holds that
processed information, for example, the total number of rows updated.
A cursor in PL/SQL gives a name and acts as a pointer to the area of work called a context
area and then uses its information. It keeps the number of rows processed by the SQL
statement. These rows are called as an active set. The size of the active set is equal to the
count of the rows that meet the condition.
2. Explicit cursor
Implicit cursors are automatically created by Oracle whenever an SQL statement is executed,
when there is no explicit cursor for the statement. Programmers cannot control the implicit
cursors and the information in it.
Whenever a DML statement (INSERT, UPDATE and DELETE) is issued, an implicit cursor
is associated with this statement. For INSERT operations, the cursor holds the data that needs
to be inserted. For UPDATE and DELETE operations, the cursor identifies the rows that
would be affected.
In PL/SQL, you can refer to the most recent implicit cursor as theSQL cursor, which always has attributes su
1. %FOUND
Returns TRUE if an INSERT, UPDATE, or DELETE statement affected one or more rows or
a SELECT INTO statement returned one or more rows. Otherwise, it returns FALSE.
2. %NOTFOUND
3. %ISOPEN
Always returns FALSE for implicit cursors, because Oracle closes the SQL cursor
automatically after executing its associated SQL statement.
4. %ROWCOUNT
Example
We will be using the CUSTOMERS table
The following program will update the table and increase the salary of each customer by 500
and use the SQL%ROWCOUNT attribute to determine the number of rows affected –
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
IF sql%notfound THEN
total_rows := sql%rowcount;
END IF;
END;
/
When the above code is executed at the SQL prompt, it produces the following result
6 customers selected
If you check the records in customers table, you will find that the rows have been updated
Explicit Cursors
Explicit cursors are programmer-defined cursors for gaining more control over the context
area. An explicit cursor should be defined in the declaration section of the PL/SQL Block. It
is created on a SELECT Statement which returns more than one row.
Declaring the cursor defines the cursor with a name and the associated SELECT statement. For example
CURSOR c_customers IS
Opening the cursor allocates the memory for the cursor and makes it ready for fetching the rows returned by
OPEN c_customers;
Fetching the cursor involves accessing one row at a time. For example, we will fetch rows from the above-op
Closing the cursor means releasing the allocated memory. For example, we will close the above-opened curs
CLOSE c_customers;
Example
DECLARE
c_id customers.id%type;
c_name customer.name%type;
c_addr customers.address%type;
CURSOR c_customers is
OPEN c_customers;
LOOP
END LOOP;
CLOSE c_customers;
END;
When the above code is executed at the SQL prompt, it produces the following result
1 Ramesh Ahmedabad
2 Khilan Delhi
3 kaushik Kota
4 Chaitali Mumbai
5 Hardik Bhopal
6 Komal MP
Other References
● PL/SQL - Cursors - Tutorialspoint