我有一张桌子' TEST'有一些专栏。其中一个被称为“有效”'。这一栏有点。
TEST Table:
Id1 Id2 VALIDATED
1 1 0
1 2 0
1 3 0
2 4 0
2 5 0
2 6 0
Id1和Id2是INT值和主键。
我为此表创建了以下触发器,并为该列创建了“有效”'只有在更新时:
CREATE TRIGGER [dbo].[MY_TRIGGER]
ON [dbo].[TEST]
FOR UPDATE
AS
IF UPDATE([VALIDATED])
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
DECLARE @VALIDATED_INSERT BIT;
SELECT @VALIDATED_INSERT = [VALIDATED]
FROM INSERTED;
PRINT 'VALIDATED INSERT:'
PRINT @VALIDATED_INSERT
DECLARE @VALIDATED_TABLE BIT;
SELECT @VALIDATED_TABLE = [VALIDATED]
FROM [dbo].[TEST]
PRINT 'VALIDATED TABLE:'
PRINT @VALIDATED_TABLE
END
想象一下,我们使用下面的更新语句和字段' VALIDATED'更新具有Id1 = 1和Id2 = 1的行。最初设置为0,如上表TEST所示:
Update TEST
SET VALIDATED=1
Where Id1= 1 and Id2 = 1
1)如果我更新此列并且“有效”'到1它打印:
VALIDATED INSERT:
1
VALIDATED TABLE:
1
2)现在,VALIDATED在表中设置为1,所以如果我再次为同一行更新它(Id1 = 1和Id2 = 1):
Update TEST
SET VALIDATED=0
Where Id1= 1 and Id2 = 1
打印:
VALIDATED INSERT:
0
VALIDATED TABLE:
1
我不明白: