Friday, 3 August 2012

Oracle Cursor Examples


Note -- Below the implicitly Open  and  implicitly fetch is occure
DECLARE
emp_rec employeetest%rowtype;
CURSOR emp_cur IS
SELECT * FROM employeetest;  
BEGIN
FOR emp_rec in emp_cur LOOP 
dbms_output.put_line (emp_rec.eno || '  ' || emp_rec.ename || '  ' || emp_rec.eno);
END LOOP;
END;


Example not implicitly fetched and Open cursor example
DECLARE
    emp_rec employeetest%rowtype;
    CURSOR emp_cur IS
    SELECT *
    FROM  employeetest
    WHERE Ename='Tahir';
 BEGIN
    OPEN emp_cur;
    FETCH emp_cur INTO emp_rec;
    dbms_output.put_line (emp_rec.eno || '  ' || emp_rec.ename);
   CLOSE emp_cur;
 END;


example to get individual column in cursor

DECLARE
    V_eno    employeetest.eno%type;
    V_ename  employeetest.ename%type;

    CURSOR emp_cur IS  
    SELECT eno,ename
    FROM  employeetest;
 
 BEGIN
 OPEN emp_cur;
  For i in 1..3 Loop
    FETCH emp_cur INTO V_eno,V_ename;
   dbms_output.put_line (V_eno || '  ' || V_ename);
   END Loop;
   CLOSE emp_cur;
 END;


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

DECLARE
    V_eno    employeetest.eno%type;
    V_ename  employeetest.ename%type;
 
    CURSOR emp_cur IS   
    SELECT eno,ename
    FROM  employeetest;
   
 BEGIN
 OPEN emp_cur;
 Loop
    FETCH emp_cur INTO V_eno,V_ename;
   dbms_output.put_line (V_eno || '  ' || V_ename);
   exit when emp_cur%rowcount>10;
   END Loop;
   CLOSE emp_cur;
 END;

No comments:

Post a Comment