更新多列时如何触发

时间:2015-09-15 09:40:58

标签: triggers oracle10g

这里我想要更新任何一个列时触发更新查询。但我得到的错误表表正在变异,触发器/函数可能看不到它。

create or replace
  TRIGGER TRIGGER1 
    AFTER  UPDATE OF HOST_ID,ENABLED_FLAG,ACTIVE_FLAG,AGENT_COUNTER,USER_WAIT_FLAG ON  MONITOR_AGENT_STATUS 
  FOR EACH ROW
    BEGIN
update monitor_agent_status set active_flag='Y', enabled_flag='Y', agent_counter=0, user_flag='N';
 END;

1 个答案:

答案 0 :(得分:3)

您应该在BEFORE行触发器中设置这样的值:

create or replace
  TRIGGER TRIGGER1 
    BEFORE UPDATE OF HOST_ID,ENABLED_FLAG,ACTIVE_FLAG,AGENT_COUNTER,USER_WAIT_FLAG ON  MONITOR_AGENT_STATUS 
  FOR EACH ROW
    BEGIN
      :NEW.active_flag:='Y';
      :NEW.enabled_flag:='Y';
      :NEW.agent_counter:=0;
      :NEW.user_flag:='N';
 END;

你的第二个触发器(来自下面的评论):

CREATE OR REPLACE TRIGGER TRIGGER11 
BEFORE UPDATE OF HOST_ID ON HOST_CURR_TIME 
FOR EACH ROW 
DECLARE 
   NewHost_Time varchar(10); 
BEGIN 
   select HOST_ID 
   into   NewHost_Time 
   from   HOST_CURR_TIME 
   where  HOST_ID='ATLMB100'; 

   :new.HOST_ID:= case :new.HOST_ID 
                     when 'CCNAFE02' then NewHost_Time 
                     when 'OFCBSERV' then NewHost_Time 
                     else :new.HOST_ID 
                  end; 
END;