PL - SQL Cursor by Practical Examples
PL - SQL Cursor by Practical Examples
PL - SQL Cursor by Practical Examples
PL/SQL Cursor
Summary: in this tutorial, you will learn about PL/SQL cursor and its usage.
A cursor is a pointer that points to a result of a query. PL/SQL has two types of cursors: implicit
cursors and explicit cursors.
Implicit cursors
Oracle internally manages the whole execution cycle of implicit cursors and reveals only the
cursor’s information and statuses such as SQL%ROWCOUNT , SQL%ISOPEN , SQL%FOUND , and
SQL%NOTFOUND .
The implicit cursor is not elegant when the query returns zero or multiple rows which cause
NO_DATA_FOUND or TOO_MANY_ROWS exception respectively.
Explicit cursors
An explicit cursor is an SELECT (https://www.oracletutorial.com/oracle-basics/oracle-select/) statement
declared explicitly in the declaration section of the current block or a package specification.
For an explicit cursor, you have control over its execution cycle from OPEN , FETCH , and CLOSE .
Oracle defines an execution cycle that executes an SQL statement and associates a cursor with it.
https://www.oracletutorial.com/plsql-tutorial/plsql-cursor/ 1/8
10/17/22, 4:25 PM PL/SQL Cursor By Practical Examples
Declare a cursor
Before using an explicit cursor, you must declare it in the declaration section of a block or package
as follows:
In this syntax:
First, specify the name of the cursor after the CURSOR keyword.
Open a cursor
Before start fetching rows from the cursor, you must open it. To open a cursor, you use the
following syntax:
OPEN cursor_name;
In this syntax, the cursor_name is the name of the cursor declared in the declaration section.
When you open a cursor, Oracle parses the query, binds variables, and executes the associated SQL
statement.
Oracle also determines an execution plan, associates host variables and cursor parameters
(https://www.oracletutorial.com/plsql-tutorial/plsql-cursor-with-parameters/) with the placeholders in the SQL
statement, determines the result set, and sets the cursor to the first row in the result set.
https://www.oracletutorial.com/plsql-tutorial/plsql-cursor/ 2/8
10/17/22, 4:25 PM PL/SQL Cursor By Practical Examples
The FETCH statement places the contents of the current row into variables. The syntax of FETCH
statement is as follows:
To retrieve all rows in a result set, you need to fetch each row till the last one.
Closing a cursor
After fetching all rows, you need to close the cursor with the CLOSE statement:
CLOSE cursor_name;
However, you must explicitly close package-based cursors. Note that if you close a cursor that has
not opened yet, Oracle will raise an INVALID_CURSOR exception.
A cursor has four attributes to which you can reference in the following format:
cursor_name%attribute
1) %ISOPEN
https://www.oracletutorial.com/plsql-tutorial/plsql-cursor/ 3/8
10/17/22, 4:25 PM PL/SQL Cursor By Practical Examples
2) %FOUND
3) %NOTFOUND
3) %ROWCOUNT
The %ROWCOUNT attribute returns the number of rows fetched from the cursor. If the cursor is not
opened, this attribute returns INVALID_CURSOR .
We will use the orders and order_items tables from the sample database
(https://www.oracletutorial.com/getting-started/oracle-sample-database/) for the demonstration.
https://www.oracletutorial.com/plsql-tutorial/plsql-cursor/ 4/8
10/17/22, 4:25 PM PL/SQL Cursor By Practical Examples
SELECT customer_id,
FROM order_items
GROUP BY customer_id;
The values of the credit column are 5% of the total sales revenues.
2. Fetch customers sorted by sales in descending order and gives them new credit limits from a
budget of 1 million.
DECLARE
-- cursor
CURSOR c_sales IS
-- record
r_sales c_sales%ROWTYPE;
BEGIN
OPEN c_sales;
LOOP
https://www.oracletutorial.com/plsql-tutorial/plsql-cursor/ 5/8
10/17/22, 4:25 PM PL/SQL Cursor By Practical Examples
UPDATE
customers
SET
credit_limit =
THEN r_sales.credit
ELSE l_budget
END
WHERE
customer_id = r_sales.customer_id;
END LOOP;
CLOSE c_sales;
END;
The second variable is an explicit cursor variable named c_sales whose SELECT
(https://www.oracletutorial.com/oracle-basics/oracle-select/) statement retrieves data from the sales
view:
https://www.oracletutorial.com/plsql-tutorial/plsql-cursor/ 6/8
10/17/22, 4:25 PM PL/SQL Cursor By Practical Examples
CURSOR c_sales IS
Third, fetch each row from the cursor. In each loop iteration, we update the credit limit and
reduced the budget. The loop terminates when there is no row to fetch or the budget is
exhausted.
The following query retrieves data from the customers table to verify the update:
SELECT customer_id,
name,
credit_limit
FROM customers
Result:
https://www.oracletutorial.com/plsql-tutorial/plsql-cursor/ 7/8
10/17/22, 4:25 PM PL/SQL Cursor By Practical Examples
As you can see clearly from the result, only the first few customers have the credit limits. If you sum
up all credit limits, the total should be 1 million as shown follows:
SELECT
SUM( credit_limit )
FROM
customers;
SUM(CREDIT_LIMIT)
-----------------
1000000
Now, you should understand PL/SQL cursors including implicit and explicit cursors, and how to use
them effectively to process data, row by row, from a table.
https://www.oracletutorial.com/plsql-tutorial/plsql-cursor/ 8/8