Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2008-05-28 : 10:34:01
|
| Using a trigger that when a record is inserted it calculates a running total after the insert. Code below:CREATE TRIGGER [UPDATEOrderRebatewithRunningTotal] ON dbo.OrderRebate AFTER INSERTASDECLARE @RunningTotal DECIMAL(16, 6), @OrderNumber VARCHAR(8)UPDATE OrderRebateSET @RunningTotal = CASEWHEN @OrderNumber IS NULL THEN Ext_RebateWHEN Ord_No <> @OrderNumber THEN Ext_RebateELSE round((@RunningTotal + Ext_Rebate),2)END,RunningRebateAmt = @RunningTotal,@OrderNumber = Ord_NoMy problem is that it is not calculating the last record. Order is entered with 4 items I get the following.Ord_no ext_rebate RunningRebateAmt102 2.095 2.095102 1.744 3.84102 2.41 6.25102 .0415 NULL |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2008-05-28 : 10:44:28
|
| try thisCREATE TRIGGER [UPDATEOrderRebatewithRunningTotal] ON dbo.OrderRebateAFTER INSERTASDECLARE @RunningTotal DECIMAL(16, 6), @OrderNumber VARCHAR(8)SELECT @RunningTotal = ROUND(SUM(ISNULL(Ext_Rebate, 0)), 2)FROM OrderRebate WHERE Ord_No = @OrderNumberUPDATE OrderRebateSET RunningRebateAmt = @RunningTotalWHERE Ord_No = @OrderNumber |
 |
|
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2008-05-28 : 10:51:32
|
| That did not work. Now all values were NULL in RunningRebateAmt |
 |
|
|
|
|
|