我一直在尝试实现这个触发器一段时间并且正在取得进展(我认为!)但现在我遇到了一个突变错误。
我在这里拥有的是三个实体(与此处相关),Customer_Order(总计等),Order_Line(数量,小计等)和产品(库存,价格)。Order_line 是一个链接实体,因此一个产品可以在多个 order_lines 中,一个 customer_order 可以有多个 order_lines,但一个 order_line 在一个订单中只能出现一次,并且只能包含一个产品。触发器的目的是从 order_line 中获取小计(或我认为实际产品中的价格)和 order_line 中的数量,将它们相乘并更新新 order_line 的小计。
所以我用我的产品外键插入一个 order_line,数量为 3,价格为 4.00,触发器将两者相乘等于 12 并更新小计。现在,我认为在这里使用价格而不是 Order_line 的小计来修复突变错误是正确的(这是因为我要求触发器更新触发语句正在访问的表,对吗?),但是如何我要解决数量问题吗?数量并不总是与库存相同,它必须小于或等于库存,所以有谁知道我如何解决这个问题以从产品中选择并更新 order_line?谢谢。
CREATE OR REPLACE TRIGGER create_subtotal
BEFORE INSERT OR UPDATE ON Order_Line
for each row
DECLARE
currentSubTotal order_line.subtotal%type;
currentQuantity order_line.quantity%type;
BEGIN
select order_line.subtotal,order_line.quantity
into currentSubTotal,currentQuantity
from order_line
where product_no = :new.product_no;
IF (currentquantity>-1 ) then
update order_line set subtotal= currentSubTotal * currentQuantity where line_no=:new.line_no;
END IF;
END;
.
run
编辑:我想我可以使用 :new 语法来使用触发语句中的数量值。我会试试这个,但我仍然希望得到确认和帮助,谢谢。