如何通过引用触发表来创建TRIGGER?

时间:2017-03-16 08:29:39

标签: oracle triggers

我可以在表上创建一个AFTER TRIGGER并在我的SELECT查询中使用该表而不会出现变异表错误吗?

我要使用的查询示例。 此查询将更新某个状态名称在警报生命周期中显示的次数:

CREATE OR REPLACE TRIGGER COUNT_STEP
   AFTER INSERT
   ON STEPS
   FOR EACH ROW
   DECLARE
   V_COUNT_SETP   VARCHAR (10000);
BEGIN
     SELECT COUNT (STATUS_NAME)
       INTO V_COUNT_SETP
       FROM (SELECT A.ALERT_ID, S.STATUS_NAME
                FROM ALERTS A, ALERT_STATUSES S, STEPS ST
              WHERE     :NEW.ALERT_INTERNAL_ID = A.ALERT_INTERNAL_ID
                    AND ST.ALERT_STATUS_INTERNAL_ID = S.STATUS_INTERNAL_ID
                    AND S.STATUS_NAME IN ('Auto Escalate'))
   GROUP BY ALERT_ID;

   UPDATE ALERTS A
      SET A.COUNT = V_COUNT_ESC
    WHERE A.ALERT_INTERNAL_ID = :NEW.ALERT_INTERNAL_ID;
END;    
/

我正在插入记录的表也需要用于计算步骤出现次数,因为它存储了警报ID和它具有的所有步骤ID。

1 个答案:

答案 0 :(得分:0)

你的问题需要更加清晰。但是,根据我的理解,您需要在表上创建一个触发器,并对该表执行选择。这会给你一个突变表错误。要绕过它,您需要在该表上执行复合触发器。像这样:

create or replace trigger emp_ct
for insert on employees compound trigger 

  v_count number;  -- Add variable here

  before statement is 
  begin
    -- PERFORM YOUR SELECT AND SEND TO A VARIABLE
  end before statement;


  after each row is 
  begin
    -- DO WANT YOU WANTED TO DO. USE THE VARIABLE 
  end after each row;
end;

基本上,使用复合触发器,您可以捕获每个触发事件。通过这样做,允许查询您正在捕获的表。