我有两个表,分别称为Table_A和Table_B。我想创建一个触发器,以便每当更新Table_Apayment
中的字段时,应将一条新记录插入到Table_B中,以显示之前的总付款额和现在的总付款额之间的差异。两张表如下:
表_A
A_id | payment |
1 | 1000 |
2 | 200 |
表_B
B_id | difference |
我的触发器实现如下。我不确定如何计算这种差异:
CREATE TRIGGER trigger_Difference
ON Table_A
FOR UPDATE
AS
BEGIN
DECLARE @Difference as INT
DECLARE @PreviousDiff as INT
BEGIN
SELECT @PreviousDiff = SUM(payment)
FROM Table_A
END
if update(payment)
BEGIN
SELECT @Difference = ***don't know what to put here***
FROM inserted
INSERT INTO Table_B (difference) VALUES (@Difference)
END
END