且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 SQL Server 中创建删除前触发器?

更新时间:2023-02-05 10:20:51

在这种情况下,您可能***执行常规的after"触发器.这是处理此类情况的最常见方法.

In this situation, you're probably better off doing a regular "after" trigger. This is the most common approach to this type of situation.

类似的东西

CREATE TRIGGER TRG_AUD_DEL
ON yourTable
FOR DELETE
AS
     INSERT INTO my_audit_table  (col1, col2, ...)
     SELECT col1, col2...
     FROM DELETED 

会发生的是,当一条记录(或多条记录!)从您的表中删除时,删除的行将插入到 my_audit_tableDELETED 表是一个虚拟的包含删除前的记录的表.

What will happen is, when a record (or records!) are deleted from your table, the deleted row will be inserted into my_audit_table The DELETED table is a virtual table that contains the record(s) as they were immediately prior to the delete.

另外,请注意触发器作为删除语句上的隐式事务的一部分运行,因此如果您的删除失败并回滚,触发器也会回滚.

Also, note that the trigger runs as part of the implicit transaction on the delete statement, so if your delete fails and rolls back, the trigger will also rollback.