如何在sql中获取最后更新的记录

时间:2016-01-26 08:22:18

标签: sql-server

我有审计表。在审计表上,触发了一个用于插入,更新,删除的触发器。 我想在同一个表上的更新触发器后触发以获取其他表的数据。 为此,我需要相同的更新数据操作记录。

我怎么能做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以使用已插入和已删除的表格。

    if ((select count(*) from inserted)>0)
    //insert occurred
    begin
    do your stuff
    end

    if ((select count(*) from deleted )>0)
    //delete occurred
    begin
    do your stuff
    end

    if ((select count(*) from inserted)>0) and ((select count(*) from deleted)>0)
    //update occurred
    begin
    //do your stuff here
    end

or else you can use update() function or columns_update() function further

if(update('somecolumnname')=1 )
//above column is updated
//do your stuff

//to check multiple columns
more info here:
http://www.databasejournal.com/features/mssql/article.php/1479821/Using-ColumnsUpdated-in-a-Trigger.htm