我的触发器由于某种原因插入4次,最后一次仅被分组。基本上我想要做的是将信息插入到我的表tbl_delete_Panel
中时我想对这些数据进行分组并将分组插入tbl_delete_panel_Orderin
。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Trigger[dbo].[deletePanelSQL]
ON [dbo].[tbl_delete_Panel]
AFTER INSERT
AS
BEGIN
/* Insert Summed Quantity for each stud or track into tbl_delete_panel for deletion. tbl_delete_panel trigger will be triggered */
INSERT INTO tbl_delete_panel_Orderin (Part_Name, OrderID, SumOfQTY)
SELECT
tbl_delete_panel.Part_Name, tbl_delete_panel.OrderID,
Sum(tbl_delete_panel.QTY) AS SumOfQTY
FROM
tbl_delete_panel
GROUP BY
tbl_delete_panel.Part_Name, tbl_delete_panel.OrderID;
END;
答案 0 :(得分:1)
现在,每次触发此触发器时,您都会继续从tbl_delete_panel
插入完整的行。
你应该做什么是看Inserted
伪表可以并且将包含多行(所以你需要处理这种情况,一个不错的,正确的基于集合的方法 - 没有游标,没有while循环!) - 并插入那些行(由导致触发器触发的语句新插入的行) - 而不是每个表都是时间!
尝试这样的事情:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Trigger[dbo].[deletePanelSQL]
ON [dbo].[tbl_delete_Panel]
AFTER INSERT
AS
BEGIN
/* Insert summed quantity for each stud or track into tbl_delete_panel for deletion. tbl_delete_panel trigger will be triggered */
INSERT INTO tbl_delete_panel_Orderin (Part_Name, OrderID, SumOfQTY)
SELECT
Part_Name, OrderID,
Sum(QTY) AS SumOfQTY
FROM
Inserted i
GROUP BY
Part_Name, OrderID;
END;