我在表上觸發一個名爲StockItem的表,觸發MSSQL中的錶行更新事件。子查詢返回多個值 - 我的觸發器無法處理多個行更新
如果只更新一行(即一個產品),觸發器似乎可以工作。
然而,當有多個行更新,我得到以下錯誤:
Msg 512, Level 16, State 1, Procedure IC_ProductUpdate, Line 7
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.
這裏是我的觸發器:
ALTER TRIGGER [dbo].[IC_ProductUpdate] ON [dbo].[StockItem]
AFTER UPDATE
AS
BEGIN
-- Get Product Id
DECLARE @StockItemID INT = (SELECT ItemID FROM INSERTED);
-- Proceed If This Product Is Syncable
IF (dbo.IC_CanSyncProduct(@StockItemID) = 1)
BEGIN
-- Check If Product Was Synced
IF ((SELECT COUNT(*) FROM IC_ProductCreateQueue WHERE StockItemID = @StockItemID) > 0)
BEGIN
-- Check If Any Important Columns Was Updated
IF (UPDATE(Weight) OR UPDATE(SpareNumber1))
BEGIN
-- Check If There Is A [ProductUpdate] Queue Entry Already Exist For This Product
IF ((SELECT COUNT(*) FROM IC_ProductUpdateQueue WHERE StockItemID = @StockItemID) > 0)
BEGIN
-- Reset [ProductUpdate] Queue Entry
UPDATE IC_ProductUpdateQueue SET Synced = 0
WHERE StockItemID = @StockItemID
END
ELSE
BEGIN
-- Insert [ProductUpdate] Queue Entry
INSERT INTO IC_ProductUpdateQueue (StockItemID, Synced) VALUES
(@StockItemID, 0)
END
END
END
ELSE
BEGIN
-- Insert [ProductCreate] Queue Entry
INSERT INTO IC_ProductCreateQueue (StockItemID, Synced) VALUES
(@StockItemID, 0);
-- Insert [ProductUpdate] Queue Entry
INSERT INTO IC_ProductUpdateQueue (StockItemID, Synced) VALUES
(@StockItemID, 0);
END
END
END
什麼是處理多行更新的最佳方式在我的觸發器內?
感謝GarethD,你簡直太棒了!這個更新的觸發器完美無缺地工作。 :) !!! – Latheesan