I'm trying to rollback a transaction if the sum of enrollment for certain classes is over 50. For some reason, even though my if statement equates to true (the sum of the courses is 60) this still ends up deleting the courses.
如果某些类的注册总和超过50,我正在尝试回滚事务。出于某种原因,即使我的if语句等于true(课程总和为60),这仍然会删除课程。
Any ideas?
有任何想法吗?
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 个解决方案
#1
4
Why are you executing delete query before checking the condition?
为什么在检查条件之前执行删除查询?
If you Delete records from Course
table Where condition satisfy Term = 'C'
, then SELECT query IF (SELECT SUM(Enrollment) FROM Course WHERE Term = 'C') > 50
never going to satisfy and not rollback.
如果从课程表中删除记录,其中条件满足Term ='C',那么SELECT查询IF(SELECT SUM(Enrollment)FROM Course WHERE Term ='C')> 50永远不会满足而不是回滚。
#1
4
Why are you executing delete query before checking the condition?
为什么在检查条件之前执行删除查询?
If you Delete records from Course
table Where condition satisfy Term = 'C'
, then SELECT query IF (SELECT SUM(Enrollment) FROM Course WHERE Term = 'C') > 50
never going to satisfy and not rollback.
如果从课程表中删除记录,其中条件满足Term ='C',那么SELECT查询IF(SELECT SUM(Enrollment)FROM Course WHERE Term ='C')> 50永远不会满足而不是回滚。