我正在编写一个触发器,如下所示,该触发器从表中获取一个Auto Increment变量的值。想法是从另一个保留批准ID的表中检查该值。如果找到匹配项,则该交易被允许,否则被拒绝。
令我惊讶的是,触发器本身内,该字段的值根据捕获值的位置而变化。
以下是触发脚本。当在第一个RAISERROR中显示AutoIndex的值时,它保持稳定,但是在第二个实例中(即在IF块中)引用相同的值时,它会不断变化。请告诉我我要去哪里错了。
ALTER TRIGGER [dbo].[WIZ_TRG_DOC_APPROVAL]
ON [dbo].[InvNum]
FOR INSERT, UPDATE
AS
BEGIN
DECLARE @AID AS INT
DECLARE @APRV AS BIT
DECLARE @Msg AS VARCHAR(100)
DECLARE @DOCST AS INT
DECLARE @ORD AS VARCHAR(10)
DECLARE @INV AS VARCHAR(10)
SELECT @AID = AutoIndex, @DOCST = DocState FROM inserted
Set @Msg = 'This document is not fully authorised. ' + ' AutoIndex = ' + convert(varchar(10), @AID)
RAISERROR (@Msg, 16, 1);
IF @DOCST = 4
BEGIN
IF NOT EXISTS (SELECT Dir__Approve FROM WIZ_DOC_APPROVAL WHERE DocIndex = @AID)
SET @APRV = 0 --Approval record has not been created yet
ELSE
SELECT @APRV = ISNULL(Dir__Approve, 0) FROM WIZ_DOC_APPROVAL WHERE DocIndex = @AID
IF @APRV <> 1
BEGIN
Set @Msg = 'This document is not fully authorised. '
+ ' AutoIndex = ' + convert(varchar(10), @AID)
RAISERROR (@Msg, 16, 1);
ROLLBACK;
END
END
END