博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
oracle 游标中抛出异常的处理方式
阅读量:7031 次
发布时间:2019-06-28

本文共 2482 字,大约阅读时间需要 8 分钟。

在oracle游标的使用中,用for循环是一种较直接open 游标然后关闭游标更好的应用方式。现在写两个存储过程,验证这两种情况下游标中抛出异常后游标是否正常关闭。

现在有一张表emp,表结构如下:

SQL> describe emp; 名称                                      是否为空? 类型 ----------------------------------------- -------- ------------ ID                                                 NUMBER(38) USER_NAME                                          VARCHAR2(20) SALARY                                             NUMBER(38) EMP_DEPTNO                                         NUMBER(38)

表中的数据如下:

SQL> select * from emp;        ID USER_NAME                SALARY EMP_DEPTNO---------- -------------------- ---------- ----------         1 Zhangsan                   5200         10         2 Lisi                      11500         20         3 Wangwu                    13800         30         4 Qianliu                   12300         20         5 Chenqi                    14700         30

现在在表中查找,如果找到Lisi,就抛出一个异常,用来查看游标在异常抛出中是否正确关闭。

用for循环实现如下:

create or replace procedure cursor_exception1 as       cursor c_emp is select * from emp;       name_exception exception;       begin         for item in c_emp loop           if(item.user_name='Lisi') then             raise name_exception;           end if;         end loop;       exception         when name_exception then           open c_emp; -- 再次代开游标,看是否报错,如果没有报错,证明进入exception之前已经正确关闭           close c_emp;           dbms_output.put_line('find name Lisi');         when others then           dbms_output.put_line('Other error occured');       end cursor_exception1; /

调用该存储过程并查看输出结果:

SQL> call cursor_exception1();find name Lisi

 

用普通的loop实现:

create or replace procedure cursor_exception2 is  cursor cursor_emp is    select * from emp;  emp_row emp%rowtype;  name_exception exception;begin  open cursor_emp;  loop    fetch cursor_emp      into emp_row;    if (emp_row.user_name = 'Lisi') then      raise name_exception;    end if;    if (cursor_emp%notfound) then      exit;    end if;  end loop;  close cursor_emp;exception  when name_exception then    open cursor_emp;    -- 再次代开游标,看是否报错,如果没有报错,证明进入exception之前已经正确关闭    close cursor_emp;    dbms_output.put_line('Find name Lisi');  when others then    dbms_output.put_line('Other error occured');end; /

调用并查看输出结果:

SQL> call cursor_exception2();call cursor_exception2()     *第 1 行出现错误:ORA-06511: PL/SQL: 游标已经打开ORA-06512: 在 "SYS.CURSOR_EXCEPTION2", line 3ORA-06512: 在 "SYS.CURSOR_EXCEPTION2", line 21ORA-06510: PL/SQL: 用户定义的异常错误未得到处理

 

 

转载于:https://www.cnblogs.com/bejour/p/3370995.html

你可能感兴趣的文章
js运行机制及异步编程(二)
查看>>
typescript文档化工具——Typedoc
查看>>
JS数据结构0x004:链表
查看>>
以太坊钱包开发系列1 - 创建钱包账号
查看>>
社交系统 ThinkSNS+ V2.1.1 更新播报
查看>>
理解CPU分支预测,提高代码效率
查看>>
javascript调试接口
查看>>
Python基础系列:初识python引用计数与弱引用
查看>>
javascript继承方法以及优缺点
查看>>
tab 切换下划线跟随实现
查看>>
20+个很棒的Android开源项目
查看>>
跨域、vue双向绑定相关面试题
查看>>
Web Components(一)入门
查看>>
mpvue打包没有app.json等配置文件的解决方法
查看>>
树莓派配置swoole环境
查看>>
JavaScript 工作原理之十二-网络层探秘及如何提高其性能和安全性
查看>>
搭建基于react项目的心得
查看>>
react-native踩坑记录
查看>>
HTTP API 设计入坑指南(一)
查看>>
OkHttp源码分析
查看>>