Explicit_cursor

This is also called as user defined cursor

there are multiple ways of defining cursor

------------------------------------------------------------------------------------------------------------
Method 1:
------------------------------------------------------------------------------------------------------------
declare
cursor cur is select first_name, last_name from hr.employees;
fname varchar2(50);
lname varchar2(50);

begin
open cur;
    loop
    fetch cur into fname, lname;
        exit when cur%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE(cur%rowcount || ' : ' || fname || ' ' || lname);
    end loop ;
    close cur;   
end;
----------------------------OUTPUT---------------------------------------------
1 : Ellen Abel
2 : Sundar Ande
3 : Mozhe Atkinson
4 : David Austin
5 : Hermann Baer
....................
 
------------------------------------------------------------------------------------------------------------
Method 2:
------------------------------------------------------------------------------------------------------------  declare
cursor cur is select first_name, last_name from hr.employees;
begin
    for c1 in cur loop
        dbms_output.put_line(c1.first_name ||' ' || c1.last_name ||' : ' ||  cur%rowcount);
    end loop;
end ;
----------------------------OUTPUT---------------------------------------------
Ellen Abel : 1
Sundar Ande : 2
Mozhe Atkinson : 3
David Austin : 4
Hermann Baer : 5
........................

------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment