SQL 嵌套事务

SQL Nested transaction

需要对以下查询进行解释。 在提交外部事务 T 时,该行已被嵌套事务删除。但是仍然选择并显示ID为2的值。

BEGIN TRAN T
SELECT * from tbl_types where ID=2

    BEGIN TRAN nested
       DELETE from tbl_types where ID=2
    COMMIT TRAN nested

COMMIT TRAN T

第一个select结果集已经发送回客户端

之后发生了什么并不重要; next 查询将找不到自删除记录的匹配项。

这与语句排序有关。以下没有任何 nested/explicit 个事务,将产生相同的结果:

SELECT * from tbl_types where ID=2
DELETE from tbl_types where ID=2

虽然这 return 没有结果:

DELETE from tbl_types where ID=2
SELECT * from tbl_types where ID=2