简体   繁体   中英

Update Column in a table using triggers while inserting value in another table

So basically i have these 3 tables with their columns,

tbl_equipment ----> ( equip_id , equip_price )

tbl_equiporder ----> ( equip_id , proj_no , qty )

tbl_project -----> ( proj_no , proj_balance )

i need to write a trigger that updates the proj_balance when inserting values in tbl_equiporder table.

formula that needs to be used is

balance = balance -(price*qty)

i need to get the qty value from tbl_equiporder while writing the insert statement for the same mentioned table so that i can use it to update the balance in tbl_project

i have written the following trigger to update the proj_balance on inserting in tbl_equiporder table

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;

When i wrote the insert statement

INSERT INTO tbl_equiporder VALUES (1, 101, 1);

it threw the following error

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.

Have you read up about this error? You can't select from a table that you're altering in a trigger.

Remove the SELECT and just utilise the value, which is already available.

create or replace trigger trigger_equiporder 
 after insert or update or delete on tbl_equiporder
 for each row  

declare
   t_price number(4);
   t_qty number(4);
   t_balance number(4);
begin

   select equip_price into t_price 
     from tbl_equipment@fit5043a 
    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 * :new.equip_qty);

   update tbl_project 
      set proj_balance = t_balance 
    where proj_no = :new.proj_no;

end;

I would be extremely cautious about all of this; you appear to be doing cross-database SELECTs in a trigger, which will slow it down massively. It seems as though it might be worth looking at your data model to see if it could be improved; even if it's something as simple as having a materialized view of tbl_equipment locally.

You were also selecting unnecessary data across a database link, don't. I've removed it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM