update Trigger给出无效语句

时间:2015-11-25 14:10:01

标签: sqlite triggers

我在sqlite中有这个触发器,它给出了无效的语句错误 我正在尝试在插入付款后更新表格订单。 触发器会使用付款orderid查找所有付款,对其进行汇总,并更新订单中的总计

付款表

Amount
ID
OrderID

订单表

ID
Paid
Bal

触发器

CREATE TRIGGER [update_order_total]
AFTER INSERT
ON Payments
FOR EACH ROW
BEGIN
UPDATE Orders Set Orders.Paid =(Select SUM (Amount) From Payments Where  OrderID = Orders.id ) WHere id = new.ID;
UPDATE Orders Set Orders.Bal = Orders.Paid - (Select SUM (Amount) From Payments Where  OrderID = Orders.id ) WHere id = new.ID;
END

修改

我对触发器进行了编辑,第一个语句现在有效,但第二个更新语句没有

CREATE TRIGGER [update_order_total]
    AFTER INSERT
    ON Payments
    FOR EACH ROW
BEGIN
UPDATE Orders Set Paid =(Select SUM (Amount) From Payments AS p Where p.OrderID  = Orders.ID) WHere Orders.id = new.ID;
UPDATE Orders Set Bal = Paid - (Select SUM (Amount) From Payments AS p Where p.OrderID  = Orders.ID) WHere Orders.id = new.ID;
END

请问我做错了什么?

1 个答案:

答案 0 :(得分:0)

经过几个小时的调试后,我终于用以下声明解决了自己的问题

CREATE TRIGGER [update_order_total]
AFTER INSERT
ON Payments
FOR EACH ROW
BEGIN
UPDATE Orders Set Paid =(Select SUM (Amount) From Payments AS p Where p.OrderID  = Orders.ID),PayMethod =new.PayMethod  WHere Orders.id = new.OrderID;
UPDATE Orders Set Bal = Total - (Select SUM (Amount) From Payments AS p Where p.OrderID  = Orders.ID) WHere Orders.id = new.OrderID;

END

此致