Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 仅在数据实际更新时存储信息的更新触发器?_Sql Server_Tsql_Database Trigger_Mssql Jdbc - Fatal编程技术网

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

Sql server 仅在数据实际更新时存储信息的更新触发器?,sql-server,tsql,database-trigger,mssql-jdbc,Sql Server,Tsql,Database Trigger,Mssql Jdbc,我需要在我的更新触发器中添加什么来排除实际上没有更新任何内容的更新尝试 此外,我的审核表有一个事务类型列,但我不确定如何从触发器获取事务类型并将其插入该列 CREATE TRIGGER AuditTrigger2 ON authors AFTER update AS INSERT INTO audit (trackingUser, date_time) VALUES (SYSTEM_USER, getdate()) GO 编辑:根据您的评论,您可能需要: INSERT INT

我需要在我的更新触发器中添加什么来排除实际上没有更新任何内容的更新尝试

此外,我的审核表有一个事务类型列,但我不确定如何从触发器获取事务类型并将其插入该列

CREATE TRIGGER AuditTrigger2 
ON authors
AFTER update
AS
    INSERT INTO audit (trackingUser, date_time)
    VALUES (SYSTEM_USER, getdate())
GO
编辑:根据您的评论,您可能需要:

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>

注意:是排除不更新任何内容的尝试,因此您必须比较表中的每一列,以查看其是否已更改。Inserted是包含新值的临时表,Deleted是包含旧值的临时表。

在主键上连接插入和删除的集合,并测试任何其他列中的差异,并且仅在存在此类行的情况下进行插入。事务类型的可能值是什么?“Inserted”,“updated”或“deleted”您已经创建了一个更新触发器,因此不会收到有关插入或删除的通知。
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