在游标关闭后访问游标变量%rowcount时引发无效的\u游标异常

ve7v8dk2  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(392)

所以我在oracle中使用下表

EMPNO ENAME                DEPTN JOB                  HIREDDATE     SALARY
------ -------------------- ----- -------------------- --------- ----------
   111 Narayan              R22   Electrical           26-DEC-99       5000
   108 Horen                P69   PWD                  10-DEC-95      10000
   130 Roy                  A13   Security             15-SEP-01      25000
   420 Roy                  D12   IT                   15-SEP-99       2500
   100 Allu                 A13   Security             26-JAN-15      15000

我试着用ename来计算行数。

SQL> DECLARE
  2     temp varchar(20);
  3  BEGIN
  4     SELECT EName into temp from Employee01;
  5     dbms_output.put_line(temp);
  6  EXCEPTION
  7     WHEN too_many_rows THEN
  8     dbms_output.put_line('error trying to SELECT too many rows');
  9  END;
 10  /

我想修改这个代码。

mzaanser

mzaanser1#

好吧,如果你发布你修改过的代码,而不是与问题无关的东西,会有帮助的。
干得好:
第15行-光标已关闭
第17行-使用 c1%rowcount 关闭光标时
第19-21行-搬运 INVALID_CURSOR 例外情况;我只是想传达一个信息,你可能会做一些更聪明的事情

SQL> set serveroutput on
SQL> declare
  2    cursor c1 is
  3      select empno, ename, job
  4      from emp
  5      where deptno = 10;
  6    c1r     c1%rowtype;
  7    l_cnt   number;
  8  begin
  9    open c1;
 10    loop
 11      fetch c1 into c1r;
 12      exit when c1%notfound;
 13      dbms_output.put_line(c1r.ename ||' - '|| c1r.job);
 14    end loop;
 15    close c1;
 16
 17    l_cnt := c1%rowcount;
 18
 19  exception
 20    when invalid_cursor then
 21      dbms_output.put_line('INVALID_CURSOR has been raised');
 22  end;
 23  /
CLARK - MANAGER
KING - PRESIDENT
MILLER - CLERK
INVALID_CURSOR has been raised

PL/SQL procedure successfully completed.

SQL>

相关问题