Oracle SQL trigger on update of column

后端 未结 4 801
误落风尘
误落风尘 2021-01-04 02:36

I\'m trying to create a trigger for my table which automatically adds a published date based on when a certain flag is set to \'Y\'

I don\'t have much experience wit

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-04 03:16

    Use the WHEN clause:

    create or replace
      TRIGGER ADD_CREATE_DT 
      after UPDATE of approved ON articles 
      for each row
      when (new.approved = 'Y')
      BEGIN
      :new.create_dt := sysdate;
      END;
    

    Or use IF:

    create or replace
      TRIGGER ADD_CREATE_DT 
      after UPDATE of approved ON articles 
      for each row
      BEGIN
      if :new.approved = 'Y' then
       :new.create_dt := sysdate;
      end if;
      END;
    

    In this case, WHEN is more appropriate and efficient.

提交回复
热议问题