在Oracle触发器中使用if语句

时间:2012-12-18 17:29:15

标签: sql database oracle triggers

  1. 表1:客户

    cust_no | cust_credbal | cred_status

  2. 表2:customer_credittopup

    cust_no | trans_amount

  3. 我有上面的两个表,在插入Table2以更新Table1之后,我写了一个更新触发器。因此,在Table1中添加cust_credbal + cust_credbal中的Tabl1中的新值后,trans_amount 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;
    /
    

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

NEW.CUST_CREDITBALcust_credbal

不符