我有一个触发器,无论何时创建更新,都会将学生表记录更新为StudentsSID列上的新ID。请参阅以下代码:
CREATE TRIGGER [Students_AfterInsertUpdate] ON [dbo].[Students]
AFTER INSERT, UPDATE, DELETE
AS
DECLARE @nID INT
DECLARE @nIDCounter INT
SET @nIDCounter = 0
SET @nID = 10
UPDATE Students
SET ID = @nID
WHERE SID IN
(
SELECT StudentSID FROM inserted UNION SELECT StudentSID FROM deleted
)
AND (ID IS NULL OR ID < @nID)
GO
我遇到的问题是,当多个记录同时传入触发器时,ID不会增加 - 所有多个记录将具有相同的ID值(在本例中为10)。无论如何我可以使用计数器(@nIDCounter)并为每行增加@nID的值?所以说如果有3条记录进入,它们的ID分别为10,11,12。这怎么可能?