为什么SQL事务没有得到回滚

时间:2015-07-26 20:52:30

标签: sql sql-server tsql transactions sql-server-2012

如果某些类的注册总和超过50,我试图回滚一个事务。出于某种原因,即使我的if语句等于true(这些课程的总和是60),这仍然会结束删除课程。

有什么想法吗?

BEGIN TRANSACTION T1
DELETE FROM Course WHERE Term = 'C'
    IF (SELECT SUM(Enrollment) FROM Course WHERE Term = 'C') > 50
    BEGIN;
        THROW 50002, 'Total Enrollment Greater Than 50... Rolling Back Transaction...', 1;
    ROLLBACK TRANSACTION T1;
    END;
COMMIT TRANSACTION T1

1 个答案:

答案 0 :(得分:4)

为什么在检查条件之前执行删除查询?

如果从Course表中删除记录条件满足Term = 'C',则SELECT查询IF (SELECT SUM(Enrollment) FROM Course WHERE Term = 'C') > 50永远不会满足而不回滚。