我是靠谱客的博主 勤恳草莓,这篇文章主要介绍oracle触发器遇到异常数据跳过不予执行,从而实现异常数据不变,其余数据增加;,现在分享给大家,希望可以做个参考。

例如本题

## 编写存储过程和触发器,实现对scott公司员工增 加奖金,10部门增加额为工资的20%,20部门增 加额为工资的15%,30部门增加额为工资的10%, 对于职位为MANAGER且工资高于2500的员工不 增加奖金

emp表结构如下
在这里插入图片描述

题目要求为执行存储过程时如果job = manager and sal >2500的话就不增加,然后继续增加别的。
最后的结果应该是,工作为manager且工资大于2500的员工工资不变,其他的员工工资增加。

一般在触发器中使用的raise_application_error()
但是这样的话就中断了操作,导致存储过程执行中断,一个数据也更新不了,从而达不到目的。
简单方法是,当判断成功时,直接把old表的数据放到new表中,从而实现存储过程中的update语句继续执行并且我们不想执行的数据没有变化
即:
select :old.sal into :new.sal from dual;

全部代码如下;

create or replace procedure sxy2scott(deptnoi in number)
as
begin
  CASE
    WHEN deptnoi = 10 then
      update emp set sal = sal * 1.2 where deptno = 10;
    WHEN deptnoi = 20 then
      update emp set sal = sal * 1.15 where deptno = 20;
    WHEN deptnoi = 30 then
      update emp set sal = sal * 1.1 where deptno = 30;
    end CASE;
end sxy2scott;

create or replace trigger sxy2scottto
  before update
  on emp 
  for each row
declare

begin
     if :new.sal>2500 and :new.job = 'MANAGER' then 
        select :old.sal into :new.sal from dual;
     end if;
end sxy2scott;

select * from emp;
begin
  sxy2scott(20);
  commit;
  end;
select * from emp;

执行后
在这里插入图片描述
在这里插入图片描述

最后

以上就是勤恳草莓最近收集整理的关于oracle触发器遇到异常数据跳过不予执行,从而实现异常数据不变,其余数据增加;的全部内容,更多相关oracle触发器遇到异常数据跳过不予执行内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部