专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Oracle Exception

Oracle如何用EXCEPTION实现

发布时间:2010-05-20 14:01:29 文章来源:www.iduyao.cn 采编人员:星星草

过程中一段:
select count(*) into v_count from A where 1>2;
if v_count = 0 then raise_application_error(-20001,'未找到产品');
select count(*) into v_count from B where 1>2;
if v_count = 0 then raise_application_error(-20001,'无此合同);

如何用EXCEPTION实现?自定义的EXCEPTION不怎么会用

Exception e_a,
Exception e_b,
.............
select a into v_a from A where 1>2;
if sql%notfound then raise e_a end if;
select b into v_b from B where 1>2;
if sql%notfound then raise e_b end if;
............
Exception
when e_a then raise_application_error(-20001,'未找到产品');
when e_b then raise_application_error(-20001,'无此合同');
end;

请问这么写哪不对?或者应该怎么写?



------解决方法--------------------------------------------------------
不是很明确你想做成什么样子的,底下是一个我写一个trigger时,加的一个exceptiond处理,你看看先

 

Java code
create or replace trigger tri_test_3_insert

after insert or update on test_3

for each row

declare

a exception;

--pragma exception_init(a,-1476);

v number;

pragma autonomous_transaction;

begin

select count(1) into v from test_3;

dbms_output.put_line(v||'---'||:new.name||'---'||:new.ida||'---'||:new.idb);

--raise_application_error(-11111, 'error');

commit;

Exception

When others

then

ROLLBACK;

--raise a;

raise;

end;


------解决方法--------------------------------------------------------
这里可以用raise来继续上抛已经catch到的异常

也可以用raise a,上抛指定的异常

或者用
raise_application_error(-11111, 'error');这样的方式抛一个自己定义的异常。
------解决方法--------------------------------------------------------
Exception e_a,
Exception e_b,要放在定义段中
后面的逗号要改成分号

if sql%notfound then raise e_a end if;
可以直接去掉,如果找不到数据的话直接就发生异常,下面的不会执行
可以用select count(*) into v_count from A where 1>2;来代替
判断若count为0,出发异常
------解决方法--------------------------------------------------------
这样的要求不用自定义exception吧
SQL code
declare

v_a A.a%type;

v_b B.b%type; 

begin

begin

select a into v_a from A where 1>2; 

Exception 

when NO_DATA_FOUND then raise_application_error(-20001,'未找到产品');

end;

begin

select b into v_b from B where 1>2; 

Exception 

when NO_DATA_FOUND then raise_application_error(-20001,'无此合同');

end;

end; 

/
    
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: