How to update a single table using trigger in MS SQL 2008

Posted by Yakob-Jack on Stack Overflow See other posts from Stack Overflow or by Yakob-Jack
Published on 2010-05-04T08:12:10Z Indexed on 2010/05/04 8:18 UTC
Read the original article Hit count: 204

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

© Stack Overflow or respective owner

Related posts about sql

Related posts about triggers