Oracle-循环中的异常处理

2019年09月02日 11:39 · 阅读(552) ·

[目录]

参考

Oracle学习笔记——异常处理

开发环境

名称 版本
操作系统 Windows 10 X64
Oracel win64_11gR2_database
PLSQL Developer 11.0.4.1788(64 bit)01.179332 - Unlimited user license

测试表-test_v_hwhao1

测试数据

问题描述

Oracle-实际返回的行数超出请求的行数 这个例子中,我还遇到另外一个问题

  1. declare
  2. var_costcenter_name varchar2(255);
  3. var_check_account_code varchar2(255);
  4. var_check_account_name varchar2(255);
  5. begin
  6. select COSTCENTER_NAME,CHECK_ACCOUNT_CODE,CHECK_ACCOUNT_NAME into var_costcenter_name,var_check_account_code,var_check_account_name
  7. from Test_Luoma
  8. where COSTCENTER_CODE = 'BZ505' and rownum=1 ;
  9. --打印调用结果
  10. dbms_output.put_line(var_costcenter_name || var_check_account_code || var_check_account_name);
  11. end;

如果 select 语句没有查询到数据,也是会报错的。

这里针对这个问题使用测试表 test_v_hwhao1 进行分析和测试。

异常处理

写一个脚本,循环表 test_v_hwhao1,输出大于 7 的 NO 字段

  1. begin
  2. for item in (select NO from test_v_hwhao1 order by No)
  3. loop
  4. if item.NO > 7 then
  5. dbms_output.put_line(item.NO);
  6. end if;
  7. end loop;
  8. --commit;
  9. --添加一个异常处理当没有数据的时候赋一个默认值
  10. exception
  11. when NO_DATA_FOUND then
  12. dbms_output.put_line('没找到数据');
  13. when others then
  14. dbms_output.put_line('Others Exception');
  15. end;
  16. /

输出

  1. 10
  2. 11
  3. Others Exception

根据测试数据,其实应该输出的是

  1. 10
  2. 11
  3. 8
  4. 0

而且根据语法,exception 应该放在 end 之前

循环中的异常处理

根据 怎么在循环中处理异常oracle: Cursor a; for a_1 in a loop … exception … end loop 回答,我在 loop 中加了一个 begin end

  1. begin
  2. for item in (select NO from test_v_hwhao1 order by No)
  3. loop
  4. begin
  5. if item.NO > 7 then
  6. dbms_output.put_line(item.NO);
  7. end if;
  8. --添加一个异常处理当没有数据的时候赋一个默认值
  9. exception
  10. when NO_DATA_FOUND then
  11. dbms_output.put_line('没找到数据');
  12. when others then
  13. dbms_output.put_line('Others Exception');
  14. end;
  15. end loop;
  16. end;
  17. /

输出

  1. 10
  2. 11
  3. Others Exception
  4. 8
  5. 9

更新(2019年11月13日17:44:29)

发现这个异常处理可以用在可能出错的任何地方,但是要包括在 begin end 中,例如

  1. --rownum=1 避免查询出多条数据导致报错
  2. begin
  3. select GENDER into var_gender
  4. from TEST1
  5. where STAFF_ID = item.STAFF_ID and rownum=1;
  6. --处理查询数据为空的情况
  7. exception
  8. when NO_DATA_FOUND then
  9. dbms_output.put_line('没找到性别数据,Period=' || item.PERIOD || ',staffId=' || item.STAFF_ID);
  10. when others then
  11. dbms_output.put_line('Others Exception');
  12. end;
  13. --rownum=1 避免查询出多条数据导致报错
  14. if length(item.PYCOSTC) > 0 then
  15. begin
  16. select NAME into var_name
  17. from TEST2
  18. where COSTCENTER_CODE = item.PYCOSTC and rownum=1 and PERIOD = item.PERIOD;
  19. --处理查询数据为空的情况
  20. exception
  21. when NO_DATA_FOUND then
  22. dbms_output.put_line('没找到数据,Period=' || item.PERIOD || ',PYCOSTC=' || item.PYCOSTC);
  23. when others then
  24. dbms_output.put_line('Others Exception');
  25. end;
  26. end if;

自定义异常

  1. declare
  2. l_error_code EXCEPTION;
  3. begin
  4. dbms_output.put_line('1');
  5. Raise l_error_code;
  6. dbms_output.put_line('2');
  7. Exception
  8. when l_error_code Then
  9. dbms_output.put_line('自定义异常');
  10. when others then
  11. dbms_output.put_line('其它异常');
  12. end;

输出

  1. 1
  2. 自定义异常