仅在数据实际更新时才存储信息的更新触发器?

Update trigger that only stores information if data is actually updated?

CREATE TRIGGER AuditTrigger2 
ON authors
AFTER update
AS
    INSERT INTO audit (trackingUser, date_time)
    VALUES (SYSTEM_USER, getdate())
GO

我需要向我的更新触发器添加什么才能排除实际上不更新任何内容的更新尝试?

我的审计 table 也有一个交易类型列,但我不确定如何从触发器中获取交易类型并将其插入该列。

INSERT INTO audit (trackingUser, date_time)
  select SYSTEM_USER, getdate()
  from Inserted I
  inner join Deleted D on D.id = I.id /* Use your PK condition */
  where <some condition that compares fields in I & D>

编辑:根据您的评论,您可能需要:

create trigger AuditTrigger2 on authors
AFTER insert,update,delete
AS
begin
  -- Handle Insert
  INSERT INTO audit (trackingUser, date_time, trasactionType)
    select SYSTEM_USER, getdate(), 'inserted'
    from Inserted I
    where not exists (select 1 from Deleted)
    and <some condition that compares fields in I & D>
  -- Handle Delete
  INSERT INTO audit (trackingUser, date_time, trasactionType)
    select SYSTEM_USER, getdate(), 'deleted'
    from Deleted I
    where not exists (select 1 from Inserted)
    and <some condition that compares fields in I & D>
  -- Handle Update
  INSERT INTO audit (trackingUser, date_time, trasactionType)
    select SYSTEM_USER, getdate(), 'updated'
    from Inserted I
    inner join Deleted D on D.id = I.id /* Use your PK condition */
    where <some condition that compares fields in I & D>
end
go

注意:<some condition that compares fields in I & D> 是为了排除不更新任何内容的尝试,因此您必须比较 table 中的每一列以查看其是否已更改。 Inserted 是包含新值的临时 table,Deleted 是包含旧值的临时 table。