2016-02-27 47 views
0

我想創建一個名爲購買的表的更新觸發器,它將更新總成本字段並在採購日誌表中記錄一條記錄,以指示哪個購買已更新。更新列之前的Oracle Sql觸發器

我沒有與創建觸發器多少經驗,但到目前爲止,這是我已經有了

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

創建觸發器,當我得到這個錯誤:與編譯錯誤創建 觸發。當我運行顯示錯誤時;我得到ORA-00922:缺失或無效選項

任何建議?謝謝

+1

什麼樣的數據類型是數量和單位成本?你是否確定它們與INTEGER相匹配,這就是你聲明total_cost的方式。此外,定義total_cost – Olli

+0

時不需要SET,數量是number,unitcost是整數。我創建了一個插入觸發器,因爲它是成功的,所以我懷疑這是個問題 – user2127184

+1

在這裏設置total_cost:=(:NEW.quantity *:NEW.unitcost);'開始 –

回答

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

不,我沒有任何表關係 – user2127184