我创建了一个包含以下属性的表:
VendorAccount(
Ven_AccountId,
Ven_RegNo,
Ven_TxDate,
Ven_Invoice_RefNo,
TotalAmount,
Paid_ToVen ,
Balance
)
在上表中,当我从POP up LOV中选择Ven_Invoice_RefNo
时,它会从另一个表中提供TotalAmount
。每当我在Paid_ToVen
列中输入该值时,我都希望这样应从Total_Amount
中减去Balance
,新的减去值应显示在下一个条目的TotalBalance
列中。以下是该表的报告。
我想在新条目的TotalAmount
列中更新值i-e 2300。是否有任何触发或查询。
答案 0 :(得分:1)
您可以在更新后尝试trigger
。您将需要如下逻辑。请根据您的表格使用正确的字段并进行试用。
CREATE TRIGGER trg_totalBalance AFTER UPDATE ON vendoraccount
FOR EACH ROW
BEGIN
DECLARE new_balance INT;
DECLARE new_total INT;
DECLARE new_paid INT;
SELECT balance INTO old_balance,
total INTO old_total,
paidtoven INTO new_paid
FROM vendoraccount
WHERE ven_regno = new.ven_regno
AND ven_invoice_refno = new.ven_invoice_refno;
-- depending on your current balance update logic,
-- you could wrap this update with an IF/ELSE
-- IF XYZ THEN
UPDATE vendoraccount SET totalamount = old_total + old_balance - new_paid,
balance = totalamount - new_paid
WHERE ven_regno = new.ven_regno
AND ven_invoice_refno = new.ven_invoice_refno;
-- END IF;
END;