G-4385: Never use a cursor for loop to check whether a cursor returns data.
Major
Efficiency
Reason
You might process more data than required, which leads to bad performance.
Example (bad)
DECLARE
l_employee_found BOOLEAN := FALSE;
CURSOR c_employees IS
SELECT employee_id, last_name
FROM employees;
BEGIN
<<check_employees>>
FOR r_employee IN c_employees
LOOP
l_employee_found := TRUE;
END LOOP check_employees;
END;
/
Example (good)
DECLARE
l_employee_found BOOLEAN := FALSE;
CURSOR c_employees IS
SELECT employee_id, last_name
FROM employees;
r_employee c_employees%ROWTYPE;
BEGIN
OPEN c_employees;
FETCH c_employees INTO r_employee;
l_employee_found := c_employees%FOUND;
CLOSE c_employees;
END;
/