简体   繁体   English

更新列之前的Oracle SQL触发器

[英]Oracle Sql trigger before update of column

I want to create an update trigger for a table called purchases that will update the total cost field and also log a record in the purchases log table to indicate which purchase was updated. 我想为称为采购的表创建一个更新触发器,该表将更新总成本字段,并在采购日志表中记录一条记录以指示更新了哪个采购。

I don't have much experience with creating triggers but so far this is what i've got 我没有创建触发器的丰富经验,但到目前为止,这是我所拥有的

 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;
/

I get this error when creating the trigger: trigger created with compilation errors. 创建触发器时出现此错误:触发器创建时出现编译错误。 When I run show errors; 当我运行时显示错误; I get ORA-00922: missing or invalid option 我收到ORA-00922:选项丢失或无效

Any suggestion? 有什么建议吗? Thanks 谢谢

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;
/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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