I have three tables.
- Members
- Accounts
- Transactions
I want to update Accounts.AccountBalance with the sum of all Transactions.TransactionAmount after a new transaction is inserted into the Transactions table.
The following code does not seem to work for me. Any suggestions?
CREATE TRIGGER NewTrigger
AFTER INSERT ON Transactions
FOR EACH ROW
BEGIN
UPDATE Accounts SET Accounts.AccountBalance = (
SELECT SUM(Transactions.TransactionAmount)
FROM Transactions
WHERE Accounts.AccountID=Transactions.AccountID
)
Try
Here is SQLFiddle demo.
UPDATE: Since triggers are not available to you try wrap
INSERT
andUPDATE
into a stored procedure like thisAnd then use it
Here is SQLFiddle demo for that scenario.