No_data_found异常也传播到外部块?

时间:2010-04-18 06:57:52

标签: sql oracle plsql ora-01400 ora-01403

在我的代码中,我输入了雇员表中不可用的工资,然后再次在例外块中的雇员表的主键列中插入重复的employee_id,我处理的数据没有找到异常,但我不知道{{1最后的异常也是?

OUTPUT即将来临:

No data found

这是代码:

Enter some other sal
ORA-01400: cannot insert NULL into ("SCOTT"."EMPLOYEES"."LAST_NAME")
ORA-01403: no data found  --This should not come according to logic

2 个答案:

答案 0 :(得分:5)

行为是EXCEPTIONS块中出现的错误会连接到SQLERRM,从而向上传播。我授予您it is not documented但我们可以在此处清楚地看到它:

SQL> declare
  2      v_sal t23.sal%type := 230;
  3      l_num t23.sal%type;
  4  begin
  5      begin
  6          begin
  7              select sal into l_num
  8              from t23 where sal = v_sal;
  9          exception
 10              when no_data_found then
 11                  dbms_output.put_line('inner exception::'||sqlerrm);
 12                  insert into t23  values (99, 'MR KNOX', v_sal);
 13          end;
 14      exception
 15          when dup_val_on_index then
 16              dbms_output.put_line('middle exception::'||sqlerrm);
 17              insert into t23 (id, sal) values (99, v_sal);
 18      end;
 19  exception
 20      when others then
 21          dbms_output.put_line('outer exception::'||sqlerrm);
 22  end;
 23  /
inner exception::ORA-01403: no data found
middle exception::ORA-00001: unique constraint (APC.T23_PK) violated
ORA-01403: no data found
outer exception::ORA-01400: cannot insert NULL into ("APC"."T23"."LAST_NAME")
ORA-00001: unique constraint (APC.T23_PK) violated
ORA-01403: no data found

PL/SQL procedure successfully completed.

SQL>  

注意:如果存在成功处理抛出异常的嵌套异常块,则它不会连接到SQLERRM。也就是说,SQLERRM由一堆未经处理的异常组成。

答案 1 :(得分:1)

在您的例外情况下,您尝试插入employees,但不要设置last_name列,而不是NULL

  

ORA-01400:无法插入NULL(“SCOTT”。“EMPLOYEES”。“LAST_NAME”)

ORA-01403: no data found是堆栈跟踪的一部分,由选择失败引起。


您可以为所有不可为空的列定义DEFAULT值,也可以更改插入内容:

INSERT INTO employees (employee_id, last_name, ...) Values (100, 'Scott', ...);