How to update a single table using trigger in MS SQL 2008
- by Yakob-Jack
I have a table PeroidicDeduction and the fields are ID(auto-increment),TotalDeduction(e.g.it can be loan),Paid(on which the deduction for each month),RemainingAmount,
What I want is when every time I insert or update the table---RemainingAmount will get the value of TotalDeduction-SUM(Paid)....and writ the following trigger...but dosen't work for me
CREATE TRIGGER dbo.UpdatePD
ON PeroidicDedcution
AFTER INSERT,UPDATE
AS
BEGIN
UPDATE PeroidicDedcution
SET REmaininAmoubnt=(SELECT TotalDeduction-(SELECT SUM(Paid) FROM PeroidicDeduction) FROM PeroidicDeduction)
END
NOTE: it is on a Single table