表未通过触发器和过程更新

时间:2016-06-17 13:25:21

标签: sql oracle triggers procedure

我有一个人们可以进行订单的系统,每个订单都有操作,还有一个名为cm_ord_order_action的表。有时操作失败,因此我需要创建一个触发器来获取失败的订单操作的信息,并填充一个名为cm_ord_failed_order的表。

触发器如下所示:

CREATE OR REPLACE TRIGGER CM.TRGID_CM_ORD_FAILED_ORDER
AFTER UPDATE ON CM.CM_ORD_ORDER_ACTION
FOR EACH ROW
BEGIN 
   IF (:new.STATUS = 'FA') THEN
        CM.CM_FAILED_ORDER_MLT(:new.order_unit_id, :new.order_id, :new.action_type);
   END IF;
END;
/

此触发器将参数传递给更新表的过程:

CREATE OR REPLACE PROCEDURE CM_FAILED_ORDER_MLT(
v_order_unit_id NUMBER,
v_order_id in NUMBER,
v_action_type in VARCHAR)

AS
v_lob varchar(100);
v_step varchar(100);
v_error varchar(200);

BEGIN

SELECT
    ITEM.LOB_NAME, ST.STEP_NAME, ASS.STEP_ERROR
    INTO v_lob, v_step, v_error           
FROM
    CM.CM_ORD_ORDER_ACTION OA 
    INNER JOIN CM.CM_ORD_ASSIGNMENTS ASS 
        ON OA.ORDER_UNIT_ID = ASS.ORDER_ACTION_ID 
    INNER JOIN CM.CM_ORD_PROCESS_STEP ST
        ON ST.ORD_PROCESS_STEP_ID = ASS.STEP_ID 
    INNER JOIN CM.CM_ORD_AP_ITEM ITEM
        ON ITEM.AP_SUBSCRIBER_ID = OA.AP_SUBSCRIBER_ID
WHERE ASS.COMPLETION_STATUS = 'FA'
AND OA.ORDER_ID = v_order_id
AND OA.ORDER_UNIT_ID = v_order_unit_id
GROUP BY OA.ORDER_UNIT_ID, ITEM.LOB_NAME, ST.STEP_NAME, ASS.STEP_ERROR;

INSERT INTO CM_ORD_FAILED_ORDER (ORDER_ID, FAILED_DATE, ORDER_ACTION_ID, ACTION_TYPE, LOB, STEP, ERROR)
  VALUES (v_order_id, sysdate, v_order_unit_id, v_action_type, v_lob, v_step, v_error);

END CM_FAILED_ORDER_MLT;
/

这里可能有问题,因为: A - 即使触发器是在cm_ord_order_action上更新之后,当启用触发器时,状态也不会更新,但是当我禁用触发器时,状态会更新。

B - 表格cm_ord_failed_order未填充信息。

提前致谢。

3 个答案:

答案 0 :(得分:1)

您可以通过直接在触发器中执行插入来避免脚本以某种方式忽略或丢弃的变异表错误,其中您在:NEW伪记录中更新了行中的详细信息而不必再次查询。您也可以在不需要局部变量的情况下执行insert...select

我认为这是一个粗略的翻译:

CREATE OR REPLACE TRIGGER CM.TRGID_CM_ORD_FAILED_ORDER
AFTER UPDATE ON CM.CM_ORD_ORDER_ACTION
FOR EACH ROW
WHEN (new.STATUS = 'FA')
BEGIN 

  INSERT INTO CM_ORD_FAILED_ORDER (ORDER_ID, FAILED_DATE, ORDER_ACTION_ID, ACTION_TYPE,
    LOB, STEP, ERROR)
  SELECT
    DISTINCT :new.ORDER_ID, sysdate, :new.Order_Unit_Id, :new.Action_Type,
      ITEM.LOB_NAME, ST.STEP_NAME, ASS.STEP_ERROR
  FROM
    CM.CM_ORD_ASSIGNMENTS ASS
    INNER JOIN CM.CM_ORD_PROCESS_STEP ST
        ON ST.ORD_PROCESS_STEP_ID = ASS.STEP_ID 
    CROSS JOIN CM.CM_ORD_AP_ITEM ITEM
  WHERE ASS.ORDER_ACTION_ID = :new.ORDER_UNIT_ID
  AND ASS.COMPLETION_STATUS = :new.STATUS
  AND ITEM.AP_SUBSCRIBER_ID = :new.AP_SUBSCRIBER_ID;

END CM_FAILED_ORDER_MLT;
/

DISTINCT(而不是分组)和CROSS JOIN建议您在原始查询中缺少连接条件,但没有您的表结构和数据可能不是这种情况。

或者,您可以将插入保留在过程中,但将:new AP_SUBSCRIBER_ID`作为另一个参数传递,因为这似乎是您尚未传入的变异表中唯一需要的列。 / p>

您的触发器也可以是BEFORE UPDATE而不是AFTER UPDATE

答案 1 :(得分:1)

Alex的解决方案的另一种方法是避免需要交叉连接,将程序更改为:

create or replace procedure cm_failed_order_mlt (v_order_unit_id number,
                                                 v_order_id in number,
                                                 v_action_type in varchar,
                                                 v_ap_subscriber_id in cm.cm_ord_order_action.ap_subscriber_id%type)

as
  v_lob varchar(100);
  v_step varchar(100);
  v_error varchar(200);
begin
  select distinct lob_name
  into   v_lob
  from   cm.cm_ord_ap_item
  where ap_subscriber_id = v_ap_subscriber_id;

  select distinct st.step_name, ass.step_error
  into   v_step, v_error           
  from   cm.cm_ord_assignments ass
         inner join cm.cm_ord_process_step st on st.ord_process_step_id = ass.step_id
  where  ass.completion_status = 'FA'
  and    ass.order_action_id = v_order_id
  and    oa.order_unit_id = v_order_unit_id;

  insert into cm_ord_failed_order (order_id, failed_date, order_action_id, action_type, lob, step, error)
    values (v_order_id, sysdate, v_order_unit_id, v_action_type, v_lob, v_step, v_error);

end cm_failed_order_mlt;
/

或者,要删除Alex解决方案中的交叉连接,只需将其替换为标量子查询,例如:

select (select distinct lob_name from cm.cm_ord_ap_item where ap_subscriber_id = v_ap_subscriber_id), ...

答案 2 :(得分:0)

就像@JustinCave说的那样,很明显你有变异表error

  

当我们尝试引用变量表异常时会发生   在行级触发器代码中触发查询中的表

CM_ORD_ORDER_ACTION上的触发器上,您从同一个表中进行选择。尝试在过程中重做查询而不引用CM_ORD_ORDER_ACTION