Sub topic 9
Cursors with Parameters
Parameterized cursors let you define one query and supply different values each time you open it. This avoids copying similar cursor declarations for different departments, salary ranges, or dates.
This chapter assumes that you know basic SQL and PL/SQL blocks, including variables, loops, and exception handling. You should also understand the basic lifecycle of an explicit cursor: declare it, open it, fetch rows, and close it.
Why use cursor parameters?
A cursor parameter is a value supplied when the cursor opens. The parameter can be used in the cursor's query, usually in a WHERE clause.
DECLARE
CURSOR c_emp (p_department_id NUMBER, p_min_salary NUMBER) IS
SELECT last_name, salary
FROM employees
WHERE department_id = p_department_id
AND salary >= p_min_salary
ORDER BY last_name;
r_emp c_emp%ROWTYPE;
BEGIN
OPEN c_emp(50, 3000);
LOOP
FETCH c_emp INTO r_emp;
EXIT WHEN c_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(
r_emp.last_name || ': ' || r_emp.salary
);
END LOOP;
CLOSE c_emp;
END;
/The cursor declaration defines two parameters: p_department_id and p_min_salary. The call OPEN c_emp(50, 3000) supplies their values. The query then returns employees in department 50 whose salary is at least 3000.
A cursor parameter belongs to the cursor instance created by an OPEN statement. Opening the same cursor again with different arguments creates a new result set.
Parameters in a cursor FOR loop
A cursor FOR loop opens, fetches from, and closes the cursor automatically. Pass arguments after the cursor name:
DECLARE
CURSOR c_emp (p_department_id NUMBER, p_min_salary NUMBER DEFAULT 0) IS
SELECT last_name, salary
FROM employees
WHERE department_id = p_department_id
AND salary >= p_min_salary
ORDER BY last_name;
BEGIN
FOR r_emp IN c_emp(50, 3000) LOOP
DBMS_OUTPUT.PUT_LINE(
r_emp.last_name || ': ' || r_emp.salary
);
END LOOP;
END;
/The DEFAULT 0 clause makes the second argument optional. These calls are valid:
FOR r_emp IN c_emp(50, 3000) LOOP
NULL;
END LOOP;
FOR r_emp IN c_emp(50) LOOP
NULL;
END LOOP;In the second call, p_min_salary has the value 0.
You can also use named notation. This makes calls clearer when a cursor has several parameters:
FOR r_emp IN c_emp(
p_min_salary => 3000,
p_department_id => 50
) LOOP
DBMS_OUTPUT.PUT_LINE(r_emp.last_name);
END LOOP;Named notation uses the parameter names from the cursor declaration. Positional notation depends on the declaration order.
Common errors
| Problem | Result |
|---|---|
| Too few arguments without defaults | The cursor cannot be opened |
| Too many arguments | PL/SQL raises an argument error |
| Wrong data type | Oracle may reject the call or perform implicit conversion |
| Opening with new values while already open | The cursor must be closed first |
Use parameter types that match the columns or variables used by the query. For example, %TYPE can keep a parameter aligned with a table column:
DECLARE
CURSOR c_emp (
p_department_id employees.department_id%TYPE
) IS
SELECT last_name
FROM employees
WHERE department_id = p_department_id;
BEGIN
FOR r IN c_emp(50) LOOP
DBMS_OUTPUT.PUT_LINE(r.last_name);
END LOOP;
END;
/Exercise
What is the effect of calling cemp(50) when pmin_salary has DEFAULT 0?
Show answer
The cursor uses 50 for pdepartmentid and 0 for pminsalary, so it returns all matching employees in department 50 whose salary is at least 0.