Sub topic 2
Implicit cursors: SQL%ROWCOUNT and friends
This chapter assumes you know basic SQL and can write an anonymous PL/SQL block with variables, control flow, and exception handling.
What an implicit cursor is
A cursor is a work area that Oracle uses to process a SQL statement. For every INSERT, UPDATE, DELETE, or single-row SELECT INTO that your PL/SQL code runs, Oracle creates and manages an implicit cursor automatically. You do not declare, open, fetch, or close it.
PL/SQL exposes information about the most recent SQL statement through the SQL% cursor attributes:
| Attribute | Meaning |
|---|---|
| `SQL%ROWCOUNT` | Number of rows affected or returned |
| `SQL%FOUND` | `TRUE` if the statement affected or returned at least one row |
| `SQL%NOTFOUND` | `TRUE` if the statement affected or returned no rows |
| `SQL%ISOPEN` | Whether the implicit cursor is open; normally always `FALSE` |
These attributes describe the latest SQL statement. Read them before another SQL statement replaces their values.
Checking DML results
SQL%ROWCOUNT is useful when a DML statement might match a different number of rows than expected. A statement that matches no rows is not automatically an error.
BEGIN
UPDATE employees
SET salary = salary * 1.05
WHERE department_id = 50;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' rows updated');
IF SQL%ROWCOUNT = 0 THEN
DBMS_OUTPUT.PUT_LINE('No employees matched the department');
END IF;
END;
/The first reference to SQL%ROWCOUNT reports the number of rows updated. The second reference is still valid because DBMS_OUTPUT.PUT_LINE does not execute a SQL statement. For clarity, you can save the value in a variable when you need it later.
DECLARE
l_rows PLS_INTEGER;
BEGIN
DELETE FROM employees
WHERE department_id = 99;
l_rows := SQL%ROWCOUNT;
IF SQL%FOUND THEN
DBMS_OUTPUT.PUT_LINE(l_rows || ' employees deleted');
ELSE
DBMS_OUTPUT.PUT_LINE('Nothing was deleted');
END IF;
END;
/For DML, SQL%FOUND is TRUE when at least one row was affected. SQL%NOTFOUND is TRUE when zero rows were affected.
Attributes after SELECT INTO
A SELECT INTO statement must return exactly one row. After it succeeds, SQL%ROWCOUNT is normally 1. If it returns no rows, PL/SQL raises NO_DATA_FOUND. If it returns multiple rows, PL/SQL raises TOO_MANY_ROWS.
DECLARE
l_name employees.last_name%TYPE;
l_salary employees.salary%TYPE;
BEGIN
SELECT last_name, salary
INTO l_name, l_salary
FROM employees
WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE(l_name || ': ' || l_salary);
DBMS_OUTPUT.PUT_LINE('Rows returned: ' || SQL%ROWCOUNT);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee was not found');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('The query returned too many employees');
END;
/Because Oracle closes an implicit cursor when the statement finishes, SQL%ISOPEN is generally FALSE. This attribute matters more for explicit cursors.
Common errors
- Checking
SQL%ROWCOUNTafter a later SQL statement and assuming it still describes the earlier statement. - Expecting zero-row DML to raise an exception.
- Using
SELECT INTOwhen the query can return multiple rows. - Treating
SQL%ROWCOUNTas the number of rows a cursor might eventually return. That requires an explicit cursor or a cursorFORloop.
Exercise
What do SQL%FOUND and SQL%ROWCOUNT report after an UPDATE matches no rows?
Show answer
SQL%FOUND is FALSE, SQL%NOTFOUND is TRUE, and SQL%ROWCOUNT is 0. The UPDATE itself does not raise an exception.