2012-12-18 35 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