Sub topic 1
What a Cursor Is
A cursor is a control structure that lets PL/SQL work with the result of a SQL statement. It provides a way to track the statement, the rows it returns, and the current position while those rows are processed.
When Oracle runs a SQL statement, it creates a context area. The context area is memory that stores information about the statement while it runs. This information can include the statement's status, the number of rows affected, and the current row being fetched.
A cursor is the PL/SQL interface to that context area. A cursor does not usually copy every result row into a PL/SQL variable at once. Instead, it gives your code controlled access to the rows returned by a query.
flowchart TD
A[PL/SQL block] --> B[Cursor]
B --> C[SQL context area]
C --> D[Query result rows]
B --> E[Current row and cursor status]A cursor matters when a query returns multiple rows and your PL/SQL code must process those rows one at a time or in batches. For example, a program might calculate a value for each employee, write each result to a log, or send each row to another program unit.
Prerequisites
This chapter assumes that you know basic SQL, including SELECT and DML statements such as INSERT, UPDATE, and DELETE. You should also understand the structure of an anonymous PL/SQL block, PL/SQL variables, loops, and basic exception handling.
Two kinds of cursors
PL/SQL uses two main cursor types:
- An implicit cursor is created and managed automatically by Oracle for each SQL statement.
- An explicit cursor is declared and managed by your code when you need detailed control over a multi-row query.
An UPDATE statement is a common use of an implicit cursor. After the statement runs, PL/SQL makes information about that statement available through SQL cursor attributes.
BEGIN
UPDATE employees
SET salary = salary * 1.05
WHERE department_id = 50;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' rows updated');
END;
/Here, SQL%ROWCOUNT reports how many rows the UPDATE changed. The SQL prefix refers to the most recent implicit cursor. You do not declare, open, fetch from, or close this cursor.
An explicit cursor is useful when your code must inspect several rows. Its basic lifecycle is:
- Declare the cursor and its query.
- Open the cursor to run the query.
- Fetch rows from the result.
- Close the cursor when processing is complete.
DECLARE
CURSOR c_emp IS
SELECT employee_id, last_name
FROM employees
WHERE department_id = 50;
BEGIN
OPEN c_emp;
FETCH c_emp INTO l_id, l_name;
CLOSE c_emp;
END;
/This example shows the lifecycle, but it is incomplete because lid and lname are not declared and only one row is fetched. A complete program normally uses a loop and a record to process every row. Later chapters cover those patterns.
What the cursor tracks
An explicit cursor tracks whether it is open, whether the latest fetch returned a row, and how many rows have been fetched. PL/SQL exposes this information through attributes such as %ISOPEN, %FOUND, %NOTFOUND, and %ROWCOUNT.
The cursor also tracks a current position. After a fetch succeeds, the cursor is positioned on the row that was fetched. This position supports later techniques such as updating the current row with WHERE CURRENT OF.
Exercise
Which cursor type does PL/SQL create automatically for an UPDATE statement?
Show answer
An implicit cursor. Its result can be inspected with attributes such as SQL%ROWCOUNT.