title: Strange error "Ora-01001 Invalid cursor" in procedure
body: <p>
  Yesterday I worked on a strange bug in our production procedure. 
  Execution failed on statement</p>
  
  <pre class="lang-sql prettyprint-override"><code>if v_cursor%isopen then
    close v_cursor; -- here was an error 
  end if;
  </code></pre>
  
  <p>After some digging into I discovered that problem was in subprogram that opened this cursor. I fixed bug by adding output parameter sys_refcursor in subprogram. To clarify situation consider following test code:</p>
  
  <pre class="lang-sql prettyprint-override"><code>procedure nested_test(test  number,
                          p_cur out sys_refcursor)
    is  
      procedure nested_procedure_fail is
      begin      
        open p_cur for
          select 1, 2, 3, 4
            from dual
           where 1 = 0;
      end;
  
      procedure nested_procedure_success(p_cur out sys_refcursor) is
      begin
        open p_cur for
          select 1, 2, 3, 4
            from dual
           where 1 = 0;
      end;
  
    begin
      if test = 1 then
        nested_procedure_fail;
      else
        if test = 2 then
          nested_procedure_success(p_cur =&gt; p_cur);
        else
          open p_cur for
            select 6, 7, 8, 9
              from dual
             where 1 = 1;
        end if;
      end if;
    end;
  
    procedure test_fail is
      v_cur sys_refcursor;
    begin
      nested_test(test =&gt; 1, p_cur =&gt; v_cur);
      if v_cur%isopen then
        close v_cur;
      end if;
    end;
  
    procedure test_success is
      v_cur sys_refcursor;
    begin
      nested_test(test =&gt; 2, p_cur =&gt; v_cur);
      if v_cur%isopen then
        close v_cur;
      end if;
    end;
  </code></pre>
  
  <p>If I try to run <code>test_success</code> everything is OK, but on <code>test_fail</code> I receive a message</p>
  
  <blockquote>
    <p>ORA-01001: Invalid cursor</p>
  </blockquote>
  
  <p>I cannot find any information about this. Can anyone explain why this code fails?</p>
  
  <p>Oracle version:</p>
  
  <pre class="lang-sql prettyprint-override"><code>Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
  PL/SQL Release 11.2.0.3.0 - Production
  CORE    11.2.0.3.0  Production
  TNS for Solaris: Version 11.2.0.3.0 - Production
  NLSRTL Version 11.2.0.3.0 - Production
  </code></pre>