trigger-after-update返回null

1bqhqjot  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(262)

我编写了以下触发器,以便在插入或更新任何记录时更新表中的两列:

CREATE TRIGGER test 
ON mytable
AFTER UPDATE
AS
BEGIN
    SET NOCOUNT ON;

    UPDATE t
    SET UPDATE_TIMESTAMP = GETDATE(),
        UPDATE_USER_NAME = SUSER_SNAME()
    FROM mytable t
    INNER JOIN inserted i ON t._ID = i._ID
END
GO

但是,它不会更新列 UPDATE_TIMESTAMP 以及 UPDATE_USER_NAME 并且仍然返回默认的空值。你能帮我修一下吗?谢谢!

tkclm6bt

tkclm6bt1#

我想你正面临着一个递归的触发器调用
试试这个:

CREATE TRIGGER test 
ON mytable
AFTER INSERT,UPDATE
AS
BEGIN
    SET NOCOUNT ON;

    IF TRIGGER_NESTLEVEL() > 1
        RETURN;

    UPDATE t
    SET UPDATE_TIMESTAMP = GETDATE(),
        UPDATE_USER_NAME = SUSER_SNAME()
    FROM mytable t
    INNER JOIN inserted i ON t._ID = i._ID
END
GO

相关问题