1. 程式人生 > >oracle寫一個先插入一條資料,在將該資料中一個欄位更新的觸發器的坑

oracle寫一個先插入一條資料,在將該資料中一個欄位更新的觸發器的坑

最近剛用使用觸發器,原理上應該是在插入之後再更新,原本以為是以下這種寫法,在網上也找了很多方法,結果說的都不太對.需要更新的欄位根本沒有被更新,我想應該是邏輯上的問題:

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;

所以,把這個記錄下來,也讓更多朋友不要在這個小問題上浪費很多時間...

喜歡的就隨手點個贊吧(* ̄︶ ̄)