具有预定义异常的plsql raise_application_error

时间:2018-11-20 05:10:48

标签: oracle plsql

有人可以让我知道我在这里犯了什么错误吗? 我们是否可以通过raise_application_error来预定义异常?

declare
        s1 emp.sal %type;
begin
        select sal into s1 from emp where ename='SOMDUTT';
        if no_data_found then
            raise_application_error(20001, 'somdutt is not there');
        end if;
        if(s1 > 10000) then
            raise_application_error(20002, 'somdutt is earing a lot');
        end if;
        update emp set sal=sal+500 where ename='SOMDUTT';
end;
/
  

如果没有找到数据,则
         *
      第5行出现错误:
      ORA-06550:第5行,第4列:
      PLS-00382:表达式的类型错误
      ORA-06550:第5行,第1列:
      PL / SQL:语句被忽略

1 个答案:

答案 0 :(得分:2)

将条件检查no_data_found移至异常块。

此外,您只能使用-20000-20999范围内的错误号

declare
        s1 emp.sal%type;
begin
        select sal into s1 from emp where ename='SOMDUTT';

        if s1 > 10000  then
            raise_application_error(-20002, 'somdutt is earing a lot');
        end if;
        update emp set sal=sal+500 where ename='SOMDUTT';
     EXCEPTION

     when no_data_found then
            raise_application_error(-20001, 'somdutt is not there');
end;
/
相关问题