在oracle中添加和减去列

时间:2013-08-20 13:49:10

标签: oracle plsql triggers oracle11g

我有表Inventory

"INVENTORY_CODE" VARCHAR2(20) NOT NULL ENABLE 
"ITEM_CODE"      VARCHAR2(20) NOT NULL ENABLE 
"WAREHOUSE_CODE" VARCHAR2(20) 
"CON_TON"        NUMBER(38,5) 
"IN_Q_TON"       NUMBER(38,5) 
"OR_Q_TON"       NUMBER(38,5) 
"RES_TON"        NUMBER(38,5) 
"RET_TON"        NUMBER(38,5)  
"ST_INV"         NUMBER(38,5)

和另一个表Inventory_warehouse

"CODE" VARCHAR2(20) NOT NULL ENABLE 
"ITEM_CODE" VARCHAR2(20) NOT NULL ENABLE 
"QUANTITY_AV_TON" NUMBER(38,5) 
"QUANTITY_AV_REAM" NUMBER(38,5) 
"QUANTITY_AV_SHEET" NUMBER(38,5)

我想触发计算QUANTITY_AV_TON

as

QUANTITY_AV_TON = IN_Q_TON + RES_TON + RET_TON + ST_INV - CON_TON - OR_Q_TON

我创建了此触发器,但它无法正常工作

create or replace trigger QUANTITY_TON
AFTER insert or update or delete on INVENTORY
for each row
begin
  UPDATE INVENTORY_WAREHOUSE
     SET QUANTITY_AV_TON =
         select (IN_Q_TON + RES_TON + RET_TON + ST_INV - CON_TON -OR_Q_TON)
         from inventory
         where INVENTORY.item_code = INVENTORY_WAREHOUSE.item_code;
end;

使用触发器从其他表计算IN_Q_TONRES_TONRET_TONST_INVCON_TONOR_Q_TON的值

1 个答案:

答案 0 :(得分:0)

如果要根据正在处理的触发器中的行的上下文值执行计算。这通常通过使用“:NEW”变量来完成。我已经编辑了触发器,但无法运行它,所以请试一试。请阅读Oracle Trigger Docs

CREATE OR REPLACE TRIGGER QUANTITY_TON
   AFTER INSERT OR UPDATE OR DELETE
   ON INVENTORY
   FOR EACH ROW
BEGIN
   UPDATE INVENTORY_WAREHOUSE
      SET QUANTITY_AV_TON =   :new.IN_Q_TON
                            + :new.RES_TON
                            + :new.RET_TON
                            + :new.ST_INV
                            - :new.CON_TON
                            - :new.OR_Q_TON
   WHERE INVENTORY_WAREHOUSE.ITEM_CODE = :new.ITEM_CODE;

END;