触发将“ SELECT SUM”插入表中。从另一张桌子 [英] Trigger to insert into a table a "SELECT SUM" from another table
本文介绍了触发将“ SELECT SUM”插入表中。从另一张桌子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这两个表:
数据在表中,AUX具有重复的日期,而表a中的日期必须没有重复,但必须加上重复的日期的总和。
The data in the table a AUX has duplicated dates, and the table a, must have the dates with no duplicates, BUT adding the total of the duplicated dates.
ej。 2017年5月1日= 123 + 123 + 123。
ej. 05/01/2017 = 123 + 123 + 123.
我认为,当aux表中的数据包含新数据时,触发器应执行此操作。
I'm thinks that a trigger should do the job when the data in the table a aux has new data.
推荐答案
CREATE TRIGGER [dbo].[trgAfterInsert] ON [dbo].[Table_a_aux]
After Insert
AS
BEGIN
Declare @dDate as Date;
Declare @iTotal as int;
Select
@dDate = i.[date],
@iTotal = i.total
From inserted i;
IF EXISTS (Select [date] from Table_a where [date] = @dDate)
BEGIN
Update Table_a
SET total = total + @iTotal
WHERE
[date] = @dDate
END
ELSE
BEGIN
INSERT INTO Table_a ([date],total) Values (@dDate,@iTotal)
END
这篇关于触发将“ SELECT SUM”插入表中。从另一张桌子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文