SQL 服务器触发器隔离/范围文档

SQL Server Trigger Isolation / Scope Documentation

我一直在寻找关于 中触发器的隔离级别(或并发性或作用域......我不确定究竟应该怎么称呼它)的文档 SQL 服务器.

我发现以下来源表明我认为是真实的(也就是说,两个用户对相同的 table 执行更新 - 即使是相同的行 - 然后将具有独立的并执行隔离触发器):

第一个问题本质上与我试图找到答案的问题相同,但给出的答案没有提供任何来源。第二个问题也切中要害,答案是一样的,但还是没有提供来源。

有人可以指出可用文档在哪里做出相同的断言吗?

谢谢!

嗯,隔离级别和作用域是两个截然不同的东西。

隔离级别
触发器在事务中运行。默认情况下,该事务应使用默认隔离级别 READ COMMITTED。但是,如果调用进程指定了不同的隔离级别,那么它将覆盖默认值。按照惯例:如果需要,您应该能够在触发器本身内覆盖它。

根据 DML Triggers 的 MSDN 页面:

The trigger and the statement that fires it are treated as a single transaction, which can be rolled back from within the trigger. If a severe error is detected (for example, insufficient disk space), the entire transaction automatically rolls back.

范围
提供的上下文是:

{来自你}

two users, executing updates to the same table --even the same rows

{来自问题中第一个链接的 MSDN 文章 "essentially the same question I am trying to find the answer to"}

Are the inserted and deleted tables scoped to the current session? In other words will they only contain the inserted and deleted records for the current scope, or will they contain the records for all current update operations against the same table? Can there even be truely concurrent operations or will locks prevent this?

在进入 inserteddeleted table 之前,应该非常清楚,在任何给定的特定行上只会发生一个 DML 操作片刻。两个或多个请求可能会在完全相同的纳秒内进入,但所有请求都将轮流执行,一次一个(是的,由于锁定)。

现在,关于 inserteddeleted table 中的内容:是的,只有该特定事件的行(甚至 可以 be) 在那两个伪 table 中。如果您执行将修改 5 行的更新,则只有这 5 行将在 inserteddeleted table 中。由于您正在寻找文档,Use the inserted and deleted Tables 的 MSDN 页面指出:

The deleted table stores copies of the affected rows during DELETE and UPDATE statements. During the execution of a DELETE or UPDATE statement, rows are deleted from the trigger table and transferred to the deleted table. The deleted table and the trigger table ordinarily have no rows in common.

The inserted table stores copies of the affected rows during INSERT and UPDATE statements. During an insert or update transaction, new rows are added to both the inserted table and the trigger table. The rows in the inserted table are copies of the new rows in the trigger table.

将此与问题的另一部分联系起来,与事务隔离级别相关的部分:事务隔离级别对 inserteddeleted tables 因为它们专门与 event/query 有关。但是,在这两个伪 table 中捕获的该操作的净效果,如果其他进程使用 READ UNCOMMITTED 隔离级别或 NOLOCK table 提示。

为了澄清一些事情,上面链接的关于 inserteddeleted table 的 MSDN 页面在一开始就声明它们是 "in memory" 但是不完全正确。从 SQL Server 2005 开始,那两个伪 table 实际上是基于 tempdbtempdb Database 的 MSDN 页面指出:

The tempdb system database is a global resource that is available to all users connected to the instance of SQL Server and is used to hold the following:

  • ...

  • Row versions that are generated by data modification transactions for features, such as: online index operations, Multiple Active Result Sets (MARS), and AFTER triggers.

在 SQL Server 2005 之前,inserteddeleted table 是从事务日志中读取的(我相信)。


总而言之,inserteddeleted tables:

  • 在事务中操作
  • 是静态的(即只读)tables
  • 仅对当前触发器可见
  • 仅包含触发该触发器
  • 实例的特定event/operation/query的行