所以基本上我有这3张桌子和他们的列,
tbl_equipment
----> ( equip_id
, equip_price
)
tbl_equiporder
----> ( equip_id
, proj_no
, qty
)
tbl_project
-----> ( proj_no
, proj_balance
)
我需要编写一个触发器来更新在表中proj_balance
插入值时的值。tbl_equiporder
需要使用的公式是
balance = balance -(price*qty)
我需要在tbl_equiporder
为同一个提到的表编写插入语句时获取 qty 值,以便我可以使用它来更新余额tbl_project
我编写了以下触发器来更新插入tbl_equiporder
表中的 proj_balance
CREATE OR REPLACE TRIGGER trigger_equiporder
AFTER INSERT OR UPDATE OR DELETE ON tbl_equiporder FOR EACH ROW
DECLARE
t_equipid NUMBER(4);
t_price NUMBER(4);
t_qty NUMBER(4);
t_balance NUMBER(4);
BEGIN
SELECT equip_id, equip_price INTO t_equipid, t_price
FROM tbl_equipment@fit5043a
WHERE equip_id = :new.equip_id;
SELECT equip_qty INTO t_qty
FROM tbl_equiporder
WHERE equip_id = :new.equip_id;
SELECT proj_balance INTO t_balance
FROM tbl_project
WHERE proj_no = :new.proj_no;
t_balance := t_balance - (t_price * t_qty);
UPDATE tbl_project
SET proj_balance = t_balance
WHERE proj_no = :new.proj_no;
END;
当我写插入语句时
INSERT INTO tbl_equiporder VALUES (1, 101, 1);
它抛出了以下错误
Error starting at line 1 in command:
INSERT INTO tbl_equiporder VALUES (1,101,'11-sep-13',1000,1)
Error report:
SQL Error: ORA-04091: table S24585181.TBL_EQUIPORDER is mutating, trigger/function may not see it
ORA-06512: at "S24585181.TRIGGER_EQUIPORDER", line 9
ORA-04088: error during execution of trigger 'S24585181.TRIGGER_EQUIPORDER'
04091. 00000 - "table %s.%s is mutating, trigger/function may not see it"
*Cause: A trigger (or a user defined plsql function that is referenced in
this statement) attempted to look at (or modify) a table that was
in the middle of being modified by the statement which fired it.
*Action: Rewrite the trigger (or function) so it does not read that table.