Sub topic 8
Cursor FOR Loop with an Inline Query
This chapter assumes that you know basic SQL and PL/SQL blocks, including SELECT, WHERE, ORDER BY, variables, loops, and basic exception handling. You also know that an explicit cursor normally has an OPEN, FETCH, and CLOSE lifecycle.
A cursor FOR loop can define its query directly in the loop. This is called an inline query. PL/SQL opens the cursor, fetches each row, and closes the cursor automatically.
Basic syntax
The query goes between parentheses after IN:
BEGIN
FOR r IN (
SELECT department_id, COUNT(*) AS cnt
FROM employees
GROUP BY department_id
ORDER BY department_id
)
LOOP
DBMS_OUTPUT.PUT_LINE(
r.department_id || ': ' || r.cnt
);
END LOOP;
END;
/Here, r is an implicitly declared record. A record is a PL/SQL variable that contains several related fields. PL/SQL creates one field for each expression in the select list:
r.department_idcomes from thedepartment_idexpression.r.cntcomes from theCOUNT(*) AS cntexpression.
The alias cnt is important. Without an alias, an expression such as COUNT(*) does not provide a convenient field name to reference.
The loop runs once for each row returned by the query. If the query returns no rows, the loop body does not run.
What PL/SQL manages automatically
An inline cursor FOR loop performs these steps:
- It opens the cursor for the query.
- It fetches one row into the loop record.
- It runs the loop body.
- It repeats until there are no more rows.
- It closes the cursor.
You do not declare a cursor name, a record type, or a separate variable for each selected column. The loop record exists only inside the loop. You cannot refer to r after END LOOP.
Naming and shaping the record
Use clear column aliases when the query contains expressions:
BEGIN
FOR r IN (
SELECT employee_id,
salary * 1.05 AS proposed_salary
FROM employees
WHERE department_id = 50
ORDER BY employee_id
)
LOOP
DBMS_OUTPUT.PUT_LINE(
r.employee_id || ': ' || r.proposed_salary
);
END LOOP;
END;
/The query determines the record's fields. If you add, remove, or rename a select-list expression, update every reference to the record fields in the loop body.
A cursor FOR loop also handles cleanup when the loop ends because of an exception. If the loop body raises an error, PL/SQL closes the cursor before propagating the exception. This makes the inline form safer and shorter than manually managed fetch code.
Inline query or named cursor?
Use an inline query when the query is used in one place and its definition is easy to understand. Use a named cursor when the query needs parameters, has a meaningful reusable name, or is shared by several statements in the same program unit.
Both forms create a cursor FOR loop record automatically. The main difference is where the query is defined.
Exercise
What fields are available in the loop record for SELECT last_name, salary * 1.1 AS adjusted_salary FROM employees?
Show answer
The record has r.last_name and r.adjusted_salary. The alias adjusted_salary supplies the field name for the calculated expression.