Oracle Cursors
Parameterized "FOR loop" Cursor
DECLARE
CURSOR c_emp_to_be_raised(p_sal emp.sal%TYPE) IS
SELECT * FROM emp WHERE sal < p_sal;
BEGIN
FOR cRowEmp IN c_emp_to_be_raised(1000) LOOP
DBMS_OUTPUT.Put_Line(cRowEmp .eName ||' ' ||cRowEmp.sal||'... should be raised ;)');
END LOOP;
END;
/
Implicit "FOR loop" cursor
BEGIN
FOR x IN (SELECT * FROM emp WHERE sal < 100) LOOP
DBMS_OUTPUT.Put_Line(x.eName ||' '||x.sal||'... should REALLY be raised :D');
END LOOP;
END;
/
.First advantage is there is no tedious declaration to do (think of this horrible "CURSOR" thing you had in previous versions)
.second advantage is you first build your select query, then when you have what you want, you immediately can access the fields of your query (x.
.The loop opens the cursor and fetches one record at a time for every loop. At the end of the loop the cursor is closed.
.Implicit cursors are faster because the interpreter's work grows as the code gets longer. The less code the less work the interpreter has to do.