0
  1. 表1:客户

    cust_no | cust_credbal | 信用状态

  2. 表2:customer_credittopup

    cust_no | 交易金额

我有上面两个表,我在插入到更新后写了一个更新Table2触发器Table1。因此,Table1 cust_credbal在添加cust_credbalin Tabl1+ in 中的新值后, trans_amountin的金额会更新Table2。如何在更新后添加 if 语句以检查是否cust_credbal大于 20 则cred_status设置为“有效”否则“已耗尽”

我的触发器显示此错误:第 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;
/
4

2 回答 2

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;
于 2012-12-18T17:36:41.503 回答
0

NEW.CUST_CREDITBAL不匹配cust_credbal

于 2012-12-18T17:33:03.500 回答