Skip to Main Content
  • Questions
  • Why i am getting invalid cursor error/

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Sujata Kumari.

Asked: February 06, 2017 - 5:55 am UTC

Last updated: November 30, 2017 - 2:07 pm UTC

Version: ORACLE 11G RELEASE 2

Viewed 10K+ times! This question is

You Asked

DECLARE
                CURSOR C_Emp
                IS
                SELECT Ename,Deptno FROM Emp WHERE Ename = 'RINTU';
        V_Ename Emp.Ename%TYPE;
        V_Deptno Emp.Deptno%TYPE;
        BEGIN
  DBMS_OUTPUT.PUT_LINE('LOOP START');
   IF C_Emp%ISOPEN  THEN
  close c_emp;
  DBMS_OUTPUT.PUT_LINE('CURSOR IS OPEN');
  END IF;
  IF C_Emp%FOUND THEN
  DBMS_OUTPUT.PUT_LINE('cond');
        /*FOR I IN C_Emp
        LOOP
        DBMS_OUTPUT.PUT_LINE(I.Ename || I.Deptno);
        END LOOP;*/
  
  ELSE
        DBMS_OUTPUT.PUT_LINE('Data not avilable');
  END IF;
  CLOSE C_Emp;
        EXCEPTION
        WHEN NO_DATA_FOUND THEN
        DBMS_OUTPUT.PUT_LINE('No data found inside the block');
        END;
  

and Connor said...

SQL> DECLARE
  2                  CURSOR C_Emp
  3                  IS
  4                  SELECT Ename,Deptno FROM Emp WHERE Ename = 'RINTU';
  5          V_Ename Emp.Ename%TYPE;
  6          V_Deptno Emp.Deptno%TYPE;
  7          BEGIN
  8    DBMS_OUTPUT.PUT_LINE('LOOP START');
  9     IF C_Emp%ISOPEN  THEN
 10    close c_emp;
 11    DBMS_OUTPUT.PUT_LINE('CURSOR IS OPEN');
 12    END IF;
 13    IF C_Emp%FOUND THEN
 14    DBMS_OUTPUT.PUT_LINE('cond');
 15          /*FOR I IN C_Emp
 16          LOOP
 17          DBMS_OUTPUT.PUT_LINE(I.Ename || I.Deptno);
 18          END LOOP;*/
 19
 20    ELSE
 21          DBMS_OUTPUT.PUT_LINE('Data not avilable');
 22    END IF;
 23    CLOSE C_Emp;
 24          EXCEPTION
 25          WHEN NO_DATA_FOUND THEN
 26          DBMS_OUTPUT.PUT_LINE('No data found inside the block');
 27          END;
 28  /
DECLARE
*
ERROR at line 1:
ORA-01001: invalid cursor
ORA-06512: at line 13




You *closed* the cursor (line 10) and then you tried to use an attribute of an open cursor (line 13). You can only use %FOUND if the cursor is open

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

thanks

A reader, November 30, 2017 - 9:49 am UTC

my problem is solved ----->thanks so much

Chris Saxon
November 30, 2017 - 2:07 pm UTC

Glad this helped.

Invalid Cursor

Trinath Pattem, July 05, 2019 - 6:36 am UTC

If we are trying to close the cursor which we are not opened yet.
In that time we are getting Invalid_cursor..If you are forget to close the cursor you will get the error like Cursor_already_open

More to Explore

DBMS_OUTPUT

More on PL/SQL routine DBMS_OUTPUT here