2012-12-18 75 views
0
  1. 表1:客戶如果使用Oracle中陳述引發

    CUST_NO | cust_credbal | cred_status

  2. 表2:customer_credittopup

    CUST_NO | trans_amount

我有以上兩個表,我已經寫了一個更新觸發器插入到Table2後更新Table1。因此Table1cust_credbal中的金額在cust_credbalTabl1 + trans_amountTable2的新值後更新。如何添加的,如果在更新後聲明,以檢查是否cust_credbal爲> 20,然後cred_status被設定爲別人的EXHAUSTED「「有效」

我的觸發顯示此錯誤:錯誤在第8行:PLS-00049:壞綁定變量 'NEW.CUST_CREDITBAL'

CREATE OR REPLACE TRIGGER after_insert_credittopup 
AFTER INSERT ON customer_credittopup 
FOR EACH ROW 
DECLARE 
    credit number(11); 
BEGIN 
    UPDATE customers 
    SET cust_creditbal=cust_creditbal +:new.trans_amount 
    WHERE cust_no=:new.cust_no; 

    SELECT Cust_creditbal INTO credit 
    FROM customers WHERE cust_no=:new.cust_no; 

    IF(:new.cust_creditbal>0) THEN 
    UPDATE customers 
     SET Cust_credstatus='Valid' 
    WHERE cust_no=:new.cust_no; 
    end if; 
end; 
/
+0

哪個表有'cust_creditbal'字段?如果它在'customers'中,':new.cust_creditbal'只會在'customers'表的觸發器中才有意義。 – a1ex07

回答

2

似乎所有你所需要的僅僅是1更新語句:

UPDATE customers 
SET cust_creditbal=cust_creditbal +:new.trans_amount, 
Cust_credstatus = CASE WHEN cust_creditbal +:new.trans_amount > 0 THEN 'Valid' 
ELSE 'Invalid' 
--or if you don't want to change status in such case just put 
--ELSE Cust_credstatus 
END 
WHERE cust_no=:new.cust_no; 
--SELECT Cust_creditbal INTO credit FROM customers WHERE cust_no=:new.cust_no; 
--IF(:new.cust_creditbal>0) THEN 
--UPDATE customers 
--SET Cust_credstatus='Valid' 
--WHERE cust_no=:new.cust_no; 
+0

這就像2個條件的魅力,如果我有4條件,如cust_credbal是> 20是有效的,小於0 overlimit,等於零用盡其他如果 – Alphy

+0

你可以在'CASE'內做到這一點:例如'當cust_creditbal +:new.trans_amount> 0 THEN'有效'當cust_creditbal +:new.trans_amount <0 THEN'超限'ELSE'用盡'END'。 – a1ex07

0

NEW.CUST_CREDITBAL不匹配cust_credbal