Tuesday, November 7, 2017

Cursors

DECLARE
  v_ename VARCHAR2(300):=NULL;
  v_job   VARCHAR2(300):=NULL;
BEGIN
  BEGIN
    SELECT ename
    INTO v_ename
    FROM emp
    WHERE empno='7839';
    /* Implicit Cursor with one row*/
    dbms_output.put_line('Implicit Cursor one row : '||v_ename);
  END;
  BEGIN
    FOR i IN
    (SELECT ename,JOB FROM emp WHERE empno IN ('7839','7698')
    )
    /* Explicit Cursor  */
    LOOP
      dbms_output.put_line(' Explicit Cursor retving more then on row ---->'||i.ename||'          '||i.job);
      FOR j IN
      (SELECT ename,JOB FROM emp WHERE ename=i.ename AND JOB=i.JOB
      )
      LOOP
        dbms_output.put_line(' Passing i values in j cursor retving more then on row ---->'||j.ename||'          '||j.JOB);
      END LOOP;
    END LOOP;
  END;
  BEGIN
    SELECT ename INTO v_ename FROM emp;
    /* Implicit Cursor */
    dbms_output.put_line('Implicit Cursor : '||v_ename);
  END;
EXCEPTION
WHEN too_many_rows THEN
  dbms_output.put_line('v_ename Implicit Cursor return Too many Rows');
END;

No comments:

Post a Comment