Sub topic 4
Cursor Attributes
This chapter assumes you know basic SQL and the structure of an anonymous PL/SQL block. Cursor attributes report the current state or result of a cursor. They help you decide whether a statement changed rows, whether a fetch succeeded, and whether a cursor is still open.
Implicit cursor attributes
Oracle creates an implicit cursor for every SQL statement that PL/SQL runs. You do not declare or manage this cursor. After a DML statement, use the SQL% attributes to inspect the most recent statement.
BEGIN
UPDATE employees
SET salary = salary * 1.05
WHERE department_id = 50;
DBMS_OUTPUT.PUT_LINE('Rows updated: ' || SQL%ROWCOUNT);
IF SQL%FOUND THEN
DBMS_OUTPUT.PUT_LINE('At least one row changed');
ELSE
DBMS_OUTPUT.PUT_LINE('No rows changed');
END IF;
END;
/The most useful implicit cursor attributes are:
| Attribute | Meaning |
|---|---|
| `SQL%FOUND` | `TRUE` when the statement affected or returned at least one row |
| `SQL%NOTFOUND` | `TRUE` when the statement affected or returned no rows |
| `SQL%ROWCOUNT` | Number of rows affected or fetched |
| `SQL%ISOPEN` | Always `FALSE` because Oracle closes an implicit cursor automatically |
Read these attributes immediately after the statement they describe. A later SQL statement can replace the implicit cursor's information.
SELECT INTO has special behavior. If it finds no row, PL/SQL raises NO_DATA_FOUND, so execution does not continue to a normal SQL%NOTFOUND test. If it finds more than one row, PL/SQL raises TOO_MANY_ROWS.
Explicit cursor attributes
An explicit cursor is declared and controlled by your code. Its attributes use the cursor name, such as c_emp%FOUND.
The normal lifecycle is:
flowchart TD
A[Declare cursor] --> B[OPEN cursor]
B --> C[FETCH a row]
C --> D{More rows?}
D -->|Yes| C
D -->|No| E[CLOSE cursor]%ISOPEN tells you whether an explicit cursor is currently open. %ROWCOUNT starts at zero after opening and increases after each successful fetch. %FOUND and %NOTFOUND describe the most recent fetch.
DECLARE
CURSOR c_emp IS
SELECT last_name, salary
FROM employees
WHERE department_id = 50
ORDER BY last_name;
l_name employees.last_name%TYPE;
l_sal employees.salary%TYPE;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO l_name, l_sal;
EXIT WHEN c_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(
c_emp%ROWCOUNT || ': ' || l_name || ' earns ' || l_sal
);
END LOOP;
DBMS_OUTPUT.PUT_LINE('Cursor open: ' || CASE
WHEN c_emp%ISOPEN THEN 'YES'
ELSE 'NO'
END);
CLOSE c_emp;
END;
/After a successful fetch, %FOUND is TRUE, %NOTFOUND is FALSE, and %ROWCOUNT increases. When a fetch attempts to read beyond the last row, %NOTFOUND becomes TRUE. That fetch does not provide a valid row, so test %NOTFOUND before processing the variables.
Common errors
Do not process fetched values before checking %NOTFOUND. Otherwise, the loop can process stale values from the previous fetch.
Do not close a cursor and then read its attributes. Referencing attributes of a closed explicit cursor can raise INVALID_CURSOR. Likewise, open a cursor before fetching from it, and close it only when it is open.
For a cursor FOR loop, Oracle performs the open, fetch, %NOTFOUND check, and close automatically. You usually need explicit attributes only when the manual lifecycle gives you information or control that the FOR loop does not provide.
Exercise
What does c_emp%ROWCOUNT represent immediately after a fetch that successfully retrieves the fourth row?
Show answer
It represents the number of rows fetched so far, so its value is 4.