For NVARCHAR2
and VARCHAR2
maximum size is 4000 bytes, or 32767 bytes if the MAX_STRING_SIZE
initialization parameter is set to EXTENDED
. This is useful you have to allocate more data to a variable.
Example of Cursor:
declare
cursor c1 is select first_name, salary from hr.employees;
begin
for c in c1
loop
dbms_output.put_line('Ename: ' || c.first_name || ', Salary: ' || c.salary);
end loop;
end;
Example of Ref Cursor
declare
c1 SYS_REFCURSOR;
ename varchar2(10);
sal number;
begin
open c1 for select first_name, salary from hr.employees;
LOOP
FETCH c1 into ename, sal;
EXIT WHEN c1%NOTFOUND;
dbms_output.put_line('Ename: ' || first_name || ', Salary: ' || salary);
END LOOP;
close c1;
end;
They are both cursors and can be processed in the same fashion and at the most basic level, they both are same. There are some important differences between regular cursors and ref cursors which are following:
A ref cursor can not be used in CURSOR FOR LOOP, it must be used in simple CURSOR LOOP statement as in example.
A ref cursor is defined at runtime and can be opened dynamically but a regular cursor is static and defined at compile time.
A ref cursor can be passed to another PL/SQL routine (function or procedure) or returned to a client. A regular cursor cannot be returned to a client application and must be consumed within same routine.
A ref cursor incurs a parsing penalty because it cannot cached but regular cursor will be cached by PL/SQL which can lead to a significant reduction in CPU utilization.
A regular cursor can be defined outside of a procedure or a function as a global package variable. A ref cursor cannot be; it must be local in scope to a block of PL/SQL code.
A regular cursor can more efficiently retrieve data than ref cursor. A regular cursor can implicitly fetch 100 rows at a time if used with CURSOR FOR LOOP. A ref cursor must use explicit array fetching.
Use of ref cursors should be limited to only when you have a requirement of returning result sets to clients and when there is NO other efficient/effective means of achieving the goal.
What are your thoughts on this post?
I’d love to hear from you! Click this link to email me—I reply to every message!
Also use the share button below if you liked this post. It makes me smile, when I see it.