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 |
|
aitai
Starting Member
3 Posts |
Posted - 2002-04-25 : 12:45:00
|
| The many that I have spoken to all are clueless on this one. Thanks in advance for the right solution!The insert trigger I created works fine (well, nearly fine), except that AFTER the first insert operation (ie second, third etc), it always produces the correct results BUT FOR THE PREVIOUS INSERTED ROW. It is as if there is a latency of one row in the temp table INSERTED.I would greatly appreciate a 'why', and more importantly, a 'how to fix it' for this problem. If you need to look at the code, it is posted below.Much appreciated--start trigger--create trigger UpdateAffiliateEarnings on Orders for insert, updateas--declare variablesdeclare @ProductType varchar(15), @AffID int, @Earnings money, @CurrentEarnings money, @AffTotalEarnings money, @AffTotalPayments money, @AffOutstandingBalance money--check existence of affiliateid, and for product typeselect @AffID=AffID, @ProductType=Source from Orders where AffID IS NOT NULL--get relevant informationif @AffID IS NOT NULLbegin if @ProductType = 'FLOWER' begin select @Earnings=CONVERT(money,ChargedAmount*CommissionRate) from Orders o,FlowerOrder t,Commission c where o.OrderID = t.OrderID and t.CommissionID = c.CommissionID and PaymentConfirmedYN='YES' end if @ProductType='PHONE' begin select @Earnings=CONVERT(money,ChargedAmount*CommissionRate) from Orders o,PhoneOrder t,Commission c where o.OrderID = t.OrderID and t.CommissionID = c.CommissionID and PaymentConfirmedYN='YES' end --update Affiliate account --get totals to update select @CurrentEarnings=AffTotalEarnings, @AffTotalPayments=AffTotalPayments from Affiliates where AffID=@AffID --calculate new totals for affiliate account set @AffTotalEarnings=@CurrentEarnings+@Earnings set @AffOutstandingBalance=@AffTotalEarnings-@AffTotalPayments --update affiliate account to new totals update Affiliates set AffTotalEarnings=@AffTotalEarnings, AffOutstandingBalance=@AffOutstandingBalance where AffID=@AffID --roll back the transaction if there is an error if @@ERROR !=0 rollback tranend-- end of trigger -- |
|
|
robvolk
Most Valuable Yak
15732 Posts |
|
|
|
|
|
|
|