简体   繁体   English

更新列时的 Oracle SQL 触发器

[英]Oracle SQL trigger on update of column

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'我正在尝试为我的表创建一个触发器,它会根据某个标志设置为“Y”的时间自动添加发布日期

I don't have much experience with creating triggers but so far this is what i've got我在创建触发器方面没有太多经验,但到目前为止,这就是我所拥有的

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

I get this error when updating the column更新列时出现此错误

trigger 'USER.ADD_CREATE_DT' is invalid and failed re-validation触发器 'USER.ADD_CREATE_DT' 无效且重新验证失败

Any ideas?有任何想法吗?

Thanks谢谢

Use the WHEN clause:使用 WHEN 子句:

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.在这种情况下,WHEN 更为合适和高效。

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;

I don't know What version of Oracle do you use?不知道你用的是什么版本的oracle? In Oracle 10g I got the following error:在 Oracle 10g 中,我收到以下错误:

ORA-04084: cannot change NEW values for this trigger type
04084. 00000 -  "cannot change NEW values for this trigger type"
*Cause:    New trigger variables can only be changed in before row
           insert or update triggers.
*Action:   Change the trigger type or remove the variable reference.

It does not allow to change the field on AFTER triggers.它不允许更改 AFTER 触发器上的字段。

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'我正在尝试为我的表创建一个触发器,该触发器根据某个标志设置为“ Y”的时间自动添加发布日期

I don't have much experience with creating triggers but so far this is what i've got我没有创建触发器的丰富经验,但到目前为止,这是我所拥有的

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

I get this error when updating the column更新列时出现此错误

trigger 'USER.ADD_CREATE_DT' is invalid and failed re-validation触发器“ USER.ADD_CREATE_DT”无效且重新验证失败

Any ideas?有任何想法吗?

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM