更新列之前的Oracle Sql触发器

时间:2016-02-27 21:11:02

标签: sql oracle oracle11g triggers

我想为名为purchase的表创建更新触发器,该表将更新总成本字段,并在采购日志表中记录记录以指示更新了哪个购买。

我没有太多创建触发器的经验,但到目前为止,这就是我所拥有的

 CREATE OR REPLACE TRIGGER Update_Purchase_Audit BEFORE
UPDATE ON Purchases
 REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
WHEN(NEW.p_id>0)
DECLARE total_cost INTEGER;
 BEGIN
   set total_cost := (:NEW.quantity * :NEW.unitcost);
   :NEW.total_cost:=total_cost*0.165;

  INSERT INTO Purchase_Log(eventdate, p_id, descrip)
   VALUES(SYSDATE, :NEW.p_id, :NEW.product);
END;
/

创建触发器时出现此错误: 使用编译错误创建的触发器。当我运行show errors时;我收到ORA-00922:缺少或无效选项

有什么建议吗?感谢

1 个答案:

答案 0 :(得分:1)

CREATE OR REPLACE TRIGGER Update_Purchase_Audit 
    BEFORE UPDATE ON Purchases
    FOR EACH ROW
    WHEN (NEW.p_id > 0)
DECLARE 
    total_cost number;
BEGIN
    total_cost := :NEW.quantity * :NEW.unitcost;
    :NEW.total_cost := total_cost * 0.165; --<< this is ok BEFORE update

    -->> Do you have table relations here? THis may need to go AFTER update
    -- in separate trigger
    INSERT INTO Purchase_Log (eventdate, p_id,      descrip)
                      VALUES(SYSDATE,   :NEW.p_id, :NEW.product);

END;
/