触发器未更新表

时间:2019-05-06 23:36:52

标签: oracle plsql

我创建了一个触发器,该触发器可以在每次更新其他表中的列时更新我创建的表。到目前为止,我的触发器已经编译,但是当我更新列时,触发器似乎没有触发或没有执行任何操作。
谁能帮我吗?


CREATE TABLE bb_sales_sum (
    idProduct number(2) NOT NULL,
    total number(6,2),
    quantity number); 


CREATE OR REPLACE TRIGGER BB_SALESUM_TRG
    AFTER UPDATE OF orderplaced on bb_basket 
    FOR EACH ROW
    WHEN (NEW.orderplaced = 1)
DECLARE 
    lv_count Number;
BEGIN   
    if :new.orderplaced = 1 then 
        for item in 
            (select idproduct, (quantity * price) AS total, quantity
            from bb_basketitem
            where idbasket = :old.idbasket)
    loop
        select count(*)
        into lv_count
        from bb_sales_sum where idProduct = item.idproduct;

        if lv_count = NULL then
            INSERT INTO bb_sales_sum
            VALUES (item.idproduct, item.total, item.quantity);
        else
            update bb_sales_sum 
            set quantity = item.quantity where
            idProduct = item.idproduct;
        end if;
    end loop;
    end if;

END; 
/
update bb_basket 
set orderplaced = 1 
where idbasket = 14;

select * from bb_sales_sum;

1 个答案:

答案 0 :(得分:0)

您可以使用类似的MERGE语句,使用bb_basketitem中的值代替for循环。

CREATE OR REPLACE TRIGGER BB_SALESUM_TRG
  AFTER UPDATE OF orderplaced on bb_basket 
    FOR EACH ROW 
 WHEN (NEW.orderplaced = 1)
BEGIN   
   MERGE INTO bb_sales_sum t USING 
      (  select idproduct, (quantity * price) AS total, quantity
                    from bb_basketitem item
                        where idbasket = :old.idbasket 
         ) s  
         ON (s.idproduct = t.idproduct ) 
             WHEN MATCHED THEN UPDATE
                SET  quantity  = s.quantity
              WHEN NOT MATCHED THEN
         INSERT (
              idproduct,quantity,total)
         VALUES
              ( s.idproduct,s.quantity,s.total );          

END; 
/

DEMO