【问题标题】:PL/SQL: My trigger doesn't work with raising exceptionsPL/SQL:我的触发器不适用于引发异常
【发布时间】:2020-12-16 04:40:42
【问题描述】:

我的代码是:

create or replace trigger trig6
before update of min_salary, max_salary on jobs2
for each row
declare
exceptie exception;
cursor c is select salary
from emp_ast
where job_id=:new.job_id;
begin
for i in c loop
if i.salary not between :new.min_salary and :new.max_salary then
raise exceptie;
end if;
end loop;

exception
when exceptie then
dbms_output.put_line('Salariile angajatilor nu se afla intre limitele salariului minim si maxim');
end;
/
update jobs2
set max_salary=1000
where job_id='SA_REP';
/

触发器编译成功,但没有引发任何异常。我尝试使用 'raise_application_error' 并且它有效,但我不知道为什么我不能对异常做同样的事情。

【问题讨论】:

  • 当您放入异常处理程序 (exception when exceptie then) 时,它的意思是“不要引发此异常”。
  • 对了,我建议你考虑indenting your code

标签: oracle plsql triggers


【解决方案1】:

按照您放置它的方式(在您发布的触发代码中),它实际上按设计“工作”:如果emp_ast 表中的salary 不在min_salaryjobs2 表中的max_salary 之间对于相同的job_id,在支持它的客户端(例如 SQL*Plus 或 SQL Developer 或 TOAD;但不在 Oracle Forms 或 Apex 中)上触发 显示 消息(使用 dbms_output.put_line),如果启用。

仅此而已。正在成功更新行。

如果你想引发一个错误,那么是的——你必须真正引发它,就像你之前对raise_application_error所做的那样。

同时,Oracle 会按照您的要求执行:检查某些条件并显示消息。这里没有什么可以阻止update

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 2015-03-14
    • 1970-01-01
    • 2021-07-26
    • 2011-08-06
    相关资源
    最近更新 更多