I need to build a trigger that passes values from the row being updated, inserted or deleted to a function(that runs beautifully) to update a denormalised running balance column. How do I pass row values in my trigger to my update SQL? Consider the following table....
Date Dealer Amount Balance
1/1/02 5 $500 $1000
2/2/02 5 $500 $1500
3/3/02 5 $500 $2000
If the user changes the amount for 2/2/02 I want to run an update that effects the balance for every date>= 2/2/02. I've got the logic right, but how do I pick up the changed row values? (Any ideas on what to do if they delete a row? It'll have to be the reverse?)
Here's my Trigger.... (inserted. is a place holder for my elusive row values)
create trigger updateSmartCashBalance
on dbo.__SmartCash
for INSERT, UPDATE, DELETE
as
/*if rows 3,4,5 are updated then... */
IF (COLUMNS_UPDATED() & 14) > 0
begin
update
dbo.__SmartCash
set balance = dbo.____Running_Balance(Inserted.Dealer, Inserted.SmartCashID, Inserted.Transaction_Date)
where
Inserted.Dealer = dbo.__SmartCash.Dealer and
Inserted.SmartCashID >= dbo.__SmartCash.SmartCashID and
Inserted.Transaction_Date >= dbo.__SmartCash.Transaction_Date
end
Date Dealer Amount Balance
1/1/02 5 $500 $1000
2/2/02 5 $500 $1500
3/3/02 5 $500 $2000
If the user changes the amount for 2/2/02 I want to run an update that effects the balance for every date>= 2/2/02. I've got the logic right, but how do I pick up the changed row values? (Any ideas on what to do if they delete a row? It'll have to be the reverse?)
Here's my Trigger.... (inserted. is a place holder for my elusive row values)
create trigger updateSmartCashBalance
on dbo.__SmartCash
for INSERT, UPDATE, DELETE
as
/*if rows 3,4,5 are updated then... */
IF (COLUMNS_UPDATED() & 14) > 0
begin
update
dbo.__SmartCash
set balance = dbo.____Running_Balance(Inserted.Dealer, Inserted.SmartCashID, Inserted.Transaction_Date)
where
Inserted.Dealer = dbo.__SmartCash.Dealer and
Inserted.SmartCashID >= dbo.__SmartCash.SmartCashID and
Inserted.Transaction_Date >= dbo.__SmartCash.Transaction_Date
end