2012-03-14 59 views
0

我创建了一个列表product_idtotal_count的库存表。当在采购表中输入采购时(purchase_idsupplier_id,product_id,price, quantity),它应更新库存表中的total_count。库存表中total_count的默认值为0.触发器通过在另一个表中添加计数/数量来更新一个表中的Total_count列

例如,当进行采购(供应商,充电器,价格,25)时,应更新库存(充电器,25)。

我无法做到这一点。这里是我的代码:

create or replace trigger trg_update_inventory 
after insert on supplier_product 
for each row 
begin 
    update inventory set total_count=total_count+ quantity where ..... 

我停留在这一点上。我如何让触发器从购买表中获取数量并更新总余额?

回答

2
CREATE OR REPLACE TRIGGER trg_update_inventory 
    AFTER INSERT ON supplier_product 
    FOR EACH ROW 
BEGIN 
    UPDATE inventory 
    SET total_count = total_count + :new.quantity 
    WHERE product_id = :new.product_id 
END; 

应该工作。然而,从系统架构的角度来看,如果存储过程make_purchase可以执行INSERTPURCHASE表和UPDATEINVENTORY表,而不是将这种逻辑放在触发器中,那么将会好得多。

相关问题