当前位置:网站首页>oracle写一个先插入一条数据,在将该数据中一个字段更新的触发器的坑

oracle写一个先插入一条数据,在将该数据中一个字段更新的触发器的坑

2022-06-25 22:06:00 langmeng110

最近刚用使用触发器,原理上应该是在插入之后再更新,原本以为是以下这种写法,在网上也找了很多方法,结果说的都不太对.需要更新的字段根本没有被更新,我想应该是逻辑上的问题:

create or replace trigger UPDATE_REDLIST_TYPE
  after insert on redlist_pass_person
  for each row
declare
  -- local variables here
  redlist_flag varchar2(10);
begin
 
 select (case
           when count(1) > 0 then
            '1'
           else
            '0'
         end)
    into redlist_flag
    from redlist_person t1
   where t1.redlist_card_no = :new.card_no;
    
update redlist_pass_person set redlist_type=redlist_flag where :old.id = :new.id;
   
end UPDATE_REDLIST_TYPE;

后面发现,是需要这样写:

create or replace trigger UPDATE_REDLIST_TYPE
  before insert on redlist_pass_person
  for each row
declare
  -- local variables here
  redlist_flag varchar2(10);
begin
 
 select (case
           when count(1) > 0 then
            '1'
           else
            '0'
         end)
    into redlist_flag
    from redlist_person t1
   where t1.redlist_card_no = :new.card_no;
    
  :new.redlist_type := redlist_flag;

   
end UPDATE_REDLIST_TYPE;

所以,把这个记录下来,也让更多朋友不要在这个小问题上浪费很多时间...

喜欢的就随手点个赞吧(* ̄︶ ̄)

原网站

版权声明
本文为[langmeng110]所创,转载请带上原文链接,感谢
https://blog.csdn.net/langmeng110/article/details/83653400