你好,游客 登录 注册 搜索
背景:
阅读新闻

ORA-01403:no data found 及 select a into b 空值

[日期:2012-12-10] 来源:Linux社区  作者:hao123yao [字体:大 中 小]

2.2. 使用异常处理

即加入异常处理

create or replace procedure pro_test is 
v_id test_table.t_id%type; 
begin 
  select t_id into v_id from test_table where rownum = 1; 
  exception 
    when no_data_found then -- no_data_found 也可以换成 others 
      null;  --其它操作。。。 
end pro_test; 

但加入异常处理后,当抛出异常,程序无法继续下面的语句。即我们不能从异常句柄再重新回到当前块。这时可以增加begin...end 模拟TRY..CATCH..块

create or replace procedure pro_test is 
  v_id test_table.t_id%type; 
begin 
  begin 
    select t_id into v_id from test_table where rownum = 1; 
  exception 
    when no_data_found then 
      -- no_data_found 也可以换成 others 
      null; --其它操作。。。 
  end; 
  insert into test_table values (1, 'test'); 
end pro_test; 

另外还有一个问题就是,当多个地方抛出同一个异常时,如果判别是哪条语句抛出的异常呢?

可以使用定位变量标记异常发生点。

create or replace procedure pro_test is 
  v_id test_table.t_id%type; 
  stmt integer := 1; 
begin 
  select t_id into v_id from test_table where rownum = 1; 
  stmt := 2; 
  select t_id into v_id from test_table where rownum = 1; 
 
exception 
  when no_data_found then   
    insert into errors values ('Error statment', stmt); 
end pro_test;

2.3. 使用游标替代

create or replace procedure pro_test is 
  v_id test_table.t_id%type; 
   
  type cursor_type is ref cursor; 
  cur cursor_type; 
begin 
  open cur for select t_id from test_table where rownum = 1; 
  fetch cur into v_id; 
  close cur; 
end pro_test; 

参考:ORA-01403:no data found exception的解决小道 http://www.linuxidc.com/Linux/2012-12/75771.htm

其他:Oracle中的Null值解析 http://www.linuxidc.com/Linux/2012-12/75772.htm

linux
相关资讯       Oracle基础知识  ORA-01403 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款