2017-02-13 88 views
-1

一个“SELECT SUM”我有这2个表:触发插入到表从另一个表

.

数据在AUX已复制日期表,表中,必须有没有重复的日期,但添加重复日期的总数。

ej。 05/01/2017 = 123 + 123 + 123.

我认为当表中的数据a aux有新数据时,触发器应该执行这项工作。

+0

提示:GROUP BY。 –

回答

0
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 
+0

谢谢,但我已经这样做了,但是如果我想从Table_a_aux编辑总数,y必须编辑2,3或n个重复的日期。 – gokufast

+0

表中的任何更改都会根据分组更改视图中的数据 – Rahul

+0

所有最佳:) – Rahul