2017-02-13 38 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

相關問題