Opening a Cursor
Opening the cursor executes the query and identifies the result set, which consists of all rows that meet the query search criteria.
The query can reference PL/SQL variables within its scope. Any variables in the query are evaluated only when the cursor is opened. In Example 6-11, each retrieved salary is multiplied by 2, even though factor is incremented after every fetch.
Example 6-11 Referencing PL/SQL Variables Within Its Scope
DECLARE
my_sal employees.salary%TYPE;
my_job employees.job_id%TYPE;
factor INTEGER := 2;
CURSOR c1 IS
SELECT factor*salary FROM employees WHERE job_id = my_job;
BEGIN
OPEN c1; -- factor initially equals 2
LOOP
FETCH c1 INTO my_sal;
EXIT WHEN c1%NOTFOUND;
factor := factor + 1; -- does not affect FETCH
END LOOP;
CLOSe c1;
END;
/
Partager