我的触发器有问题,看起来像这样:
CREATE TRIGGER [Balance_monthly]
ON [dbo].[Balance]
FOR INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON
UPDATE Months
SET Months.Balance_monthly =
CASE
WHEN Balance.In_Months > 1 THEN Amount_Monthly
END
FROM Balance
JOIN Months
ON Months.Id <= Balance.In_Months;
UPDATE Months
SET Months.Balance_monthly =
CASE
WHEN Balance.In_Months >= 13 THEN Amount_Monthly * 2
END
FROM Balance
JOIN Months
ON Months.Id <= Balance.In_Months - 12;
UPDATE Months
SET Months.Balance_monthly =
CASE
WHEN Balance.In_Months >= 25 THEN Amount_Monthly * 3
END
FROM Balance
JOIN Months
ON Months.Id <= Balance.In_Months - 24;
END
这是我的触发器存储的表格,只有一行,它会保持这种状态,所以不要考虑这一点。
正如你所看到的,这是完美的工作,直到In_Months超过37岁。我的问题是,是否有任何聪明的头脑可以帮助我如何计算它无论有多少In_Months那里是
答案 0 :(得分:1)
我认为这个表达式应该计算你想要的东西:
UPDATE Months
SET Months.Balance_monthly =
((Balance.In_Months + 12 - Months.Id) / 12) * Amount_Monthly
FROM Balance
CROSS JOIN Months;
假设如果In_Months
为1,我们应该设置1月的值。