zoukankan      html  css  js  c++  java
  • oracle显式游标不关闭、不关闭就再次打开会不会报错?

    问题:在查看老代码的时发现同一个cur第一次使用后没关闭,又第二次使用,程序使用了
    很久,一直没有发现问题,至少应用层是没有出问题。那到底oracle会不会抛出异常?

    测试:
    (A)

    create or replace procedure proc_test
    as
    type cursor_type is ref cursor;
    cur cursor_type;
    v_begin date;
    v_end date;
    begin
       open cur for
          select sysdate from dual;
         fetch cur into v_begin;
        
         open cur for ----再次open
            select sysdate from dual;
            fetch cur into v_end;

            insert into proc_use_times(PROC_NAME,PROC_DESC,BEGIN_TIME,END_TIME)
            values('测试','测试',v_begin,v_end);
            commit;
         ----而且没有关闭

    exception
      when others then
      raise_application_error(-20123,sqlcode||sqlerrm);
    end;

    begin
    proc_test;
    end;
    /
    select *
    from proc_use_times p
    where p.PROC_NAME='测试'

    ----得出一条记录并且没有抛出异常 答案是:A种情况下不会报错。

    (B)

    create or replace procedure proc_test
    as
    type cursor_type is ref cursor;
    cur cursor_type;
    v_begin date;
    v_end date;
    begin
       open cur for
          select sysdate from dual;
         fetch cur into v_begin;
        
         open cur for
            select sysdate from dual;
            fetch cur into v_end;

            insert into proc_use_times(PROC_NAME,PROC_DESC,BEGIN_TIME,END_TIME)
            values('测试','测试',v_begin,v_end);
            commit;
       close cur;----使用两次并且最后一次关闭

    exception
      when others then
      raise_application_error(-20123,sqlcode||sqlerrm);
    end

    -------------答案同A 依然没有报错

    (C)
    create or replace procedure proc_test
    as
    type cursor_type is ref cursor;
    cur cursor_type;
    v_begin date;
    v_end date;
    begin
       open cur for ----第一次打开
          select sysdate from dual;
         fetch cur into v_begin;
        
         open cur for ---第二次打开
            select sysdate from dual;
            fetch cur into v_end;

            insert into proc_use_times(PROC_NAME,PROC_DESC,BEGIN_TIME,END_TIME)
            values('测试','测试',v_begin,v_end);
            commit;
       close cur;----第一次关闭
       close cur;----第二次关闭

    exception
      when others then
      raise_application_error(-20123,sqlcode||sqlerrm);
    end

    oracle显式游标不关闭、不关闭就再次打开会不会报错?

    -----------答案是出错了。报了异常,从A和B两个测试的基础上,不难发现,C的报错也是合情合理的。

    原因是既然已经关闭了,那再次关闭的时候 就会发现找不到这个游标了@@

  • 相关阅读:
    一些Docker 操作集合
    与Flash 中国特供版斗智斗勇
    Linux 电子数据取证入门
    emu8086实现两位数加法运算
    emu8086实现两位数乘法运算
    5种三栏布局的实现方式
    通用事件绑定方法
    随机产生两个数值之间的某个值
    查询字符串中某个字符出现的位置数组
    根据对象属性的属性值,对对象进行排序
  • 原文地址:https://www.cnblogs.com/gracejiang/p/5890451.html
Copyright © 2011-2022 走看看