我有以下 2 个表:运行:
+--------+-------------+
| run_id | status |
+========+=============+
| 1 | active |
+--------+-------------+
| 2 | new |
+--------+-------------+
和命令:
+----------+--------+--------------+
| order_id | run_id | order_status |
+==========+========+==============+
| 1 | 1 | finished |
+----------+--------+--------------+
| 2 | 1 | finished |
+----------+--------+--------------+
| 3 | 1 | active |
+----------+--------+--------------+
| 4 | 2 | new |
+----------+--------+--------------+
| 5 | 2 | active |
+----------+--------+--------------+
| 6 | 2 | active |
+----------+--------+--------------+
要求实现如下逻辑:当运行中的所有订单状态相同时,应更新运行状态(与其订单相同)。
例如,当order_id = 3
状态设置为时'finished'
,run_id=1
状态也应设置为'finished'
。与order_id = 4
将状态设置'active'
为时相同。run_id = 2 should
'active'
负责检查订单状态并相应更新运行状态的程序:
CREATE OR REPLACE PROCEDURE check_and_update_run_status (in_order_id IN orders.order_id%TYPE,
in_run_id IN runs.run_id%TYPE,
in_order_status IN orders.order_status%TYPE)
AS
v_update_run VARCHAR2(1) := 'N';
BEGIN
/*query the table ORDERS and check if all orders in the given in_run_id having the same status as in_order_status: */
SELECT CASE
WHEN NOT EXISTS ( SELECT *
FROM ( SELECT order_id,
order_status
FROM orders
WHERE run_id = in_run_id )
WHERE order_status <> in_order_status )
THEN 'Y'
END
INTO v_update_run
FROM dual;
IF v_update_run THEN
UPDATE runs
SET run_status = in_order_status
WHERE run_id = in_run_id;
END IF;
END check_and_update_run_status;
我已经创建了触发器
CREATE OR REPLACE TRIGGER trigger1
AFTER INSERT OR UPDATE OF order_status ON orders FOR EACH ROW
BEGIN
check_and_update_run_status( in_order_id => :new.order_id,
in_run_id => :new.run_id,
in_po_status => :new.order_status );
END;
逻辑失败,因为错误:ORA-04091: table ORDERS is mutating, trigger/function may not see it
。触发器正在调用一个过程,该过程查询调用触发器的同一个表。
解决此类问题的最佳方法是什么?