- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试过将 COMMIT TRAN 放在 if else 循环中,但我仍然收到此错误。
我必须为一个类(class)招收一名学生。如果注册后的座位数为负数,我必须将其反转并打印一条消息说不能注册。我已经放置了其他错误消息只是为了查看事务是如何工作的。
CREATE PROCEDURE dbo.EnrollStudent ( @CourseID AS INTEGER,
@StudentID AS VARCHAR(20) ) AS
BEGIN
DECLARE @StatusID INTEGER
DECLARE @Status VARCHAR(50)
DECLARE @CurrentSeats INTEGER
DECLARE @ErrorCode INTEGER
SET @StatusID=0
IF EXISTS (SELECT 1
FROM dbo.CourseEnrollment
WHERE dbo.CourseEnrollment.CourseId=@CourseID AND dbo.CourseEnrollment.StudentId=@StudentID )
BEGIN
BEGIN TRAN Tr1
SET @StatusID = 1
SELECT @ErrorCode=@@ERROR
IF (@ErrorCode<>0) GOTO OTHERPROBLEM
ELSE
COMMIT TRAN Tr1
END
IF EXISTS ( SELECT 1
FROM dbo.CourseEnrollment
FULL OUTER JOIN dbo.Courses
ON dbo.Courses.CourseId=@CourseID
WHERE dbo.CourseEnrollment.StudentId<>@StudentID AND dbo.Courses.Faculty IS NULL )
BEGIN
BEGIN TRAN Tr2
SET @StatusID=2
SELECT @ErrorCode=@@ERROR
IF (@ErrorCode<>0) GOTO OTHERPROBLEM2
ELSE
COMMIT TRAN Tr2
END
IF @StatusID=0
BEGIN
IF EXISTS ( SELECT 1
FROM dbo.Courses
WHERE dbo.Courses.CourseId=@CourseID AND dbo.Courses.Faculty IS NOT NULL )
BEGIN
BEGIN TRAN Tr3
SET @StatusID=3
BEGIN TRAN InsertingValues
INSERT INTO dbo.CourseEnrollment (dbo.CourseEnrollment.StudentId,dbo.CourseEnrollment.CourseId)
VALUES (@StudentID,@CourseID);
SELECT @ErrorCode=@@ERROR
IF (@ErrorCode<>0) GOTO InsertProblem
ELSE
COMMIT TRAN InsertingValues
BEGIN TRAN UpdateCourses
UPDATE dbo.Courses
SET OpenSeats = OpenSeats-1
WHERE dbo.Courses.CourseId = @CourseID
SELECT @ErrorCode=@@ERROR
IF (@ErrorCode<>0) GOTO UpdateProblem
ELSE
COMMIT TRAN UpdateCourses
SELECT @CurrentSeats=OpenSeats
FROM dbo.Courses
WHERE dbo.Courses.CourseId = @CourseID
IF (@CurrentSeats<0) GOTO PROBLEM
ELSE
COMMIT TRAN Tr3
END
END
OTHERPROBLEM:
BEGIN
PRINT 'Unable to set status'
ROLLBACK TRAN
END
OTHERPROBLEM2:
BEGIN
PRINT 'Unable to set status'
ROLLBACK TRAN
END
UpdateProblem:
BEGIN
PRINT 'Not able to update values'
ROLLBACK TRAN InsertingValues
END
InsertProblem:
BEGIN
PRINT 'Not able to insert'
ROLLBACK TRAN InsertingValues
END
PROBLEM:
BEGIN
PRINT 'Seats Full!'
ROLLBACK TRAN
END
IF @StatusID = 1
BEGIN
SET @Status = 'The Student is already enrolled'
END;
ELSE IF @StatusID = 2
BEGIN
SET @Status = 'Cannot enroll until faculty is selected'
END
ELSE IF @StatusID = 3
BEGIN
SET @Status = 'Student Enrolled'
END
SELECT @Status
END;
(1 row(s) affected)
(1 row(s) affected)
Unable to set status
Msg 3903, Level 16, State 1, Procedure EnrollStudent, Line 101
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
Unable to set status
Msg 3903, Level 16, State 1, Procedure EnrollStudent, Line 108
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
Not able to update values
Msg 3903, Level 16, State 1, Procedure EnrollStudent, Line 115
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
Not able to insert
Msg 3903, Level 16, State 1, Procedure EnrollStudent, Line 123
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
Seats Full!
Msg 3903, Level 16, State 1, Procedure EnrollStudent, Line 131
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
(1 row(s) affected)
最佳答案
您收到的错误是因为您在没有打开事务的情况下回滚(您已经提交或回滚)。考虑清理存储过程的结构,尝试将整个存储过程作为一个事务执行,然后在发生错误时回滚。您还可以通过检查事务是否打开来测试是否需要回滚:
BEGIN TRANSACTION;
BEGIN TRY
--execute all your stored proc code here and then commit
COMMIT;
END TRY
BEGIN CATCH
--if an exception occurs execute your rollback, also test that you have had some successful transactions
IF @@TRANCOUNT > 0 ROLLBACK;
END CATCH
关于sql - ROLLBACK TRANSACTION 请求没有对应的 BEGIN TRANSACTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23296040/
这两行的状态设置为 1。我想将它们设置为 0 并可视化 rollback 查询的结果。这个查询完成了这项工作: START TRANSACTION; UPDATE jkm_content SET st
通常当我做 $ rake db:rollback 它回滚一次迁移。但现在,反复多次,它已经回滚了 3 个步骤。 这种行为怎么来的? 我在 Rails 3.2.13 上。 最佳答案 您可以指定这样的步骤
不确定我是否在问正确的问题,但是这里... 我已连接到关闭自动提交的 postgres 实例。我看到的是,如果查询存在语法问题: select * fro table limit 400 ,我必须先回
Cassandra 2.0 documentation包含以下关于原子性的段落: For example, if using a write consistency level of QUORUM w
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我在办公室运行本地主机 XAMPP 服务器用于测试目的。我正在旅行,决定在我的笔记本电脑上完成一些工作,因此我在我的笔记本电脑上安装了新版本的 XAMPP,复制了我的文件,但由于某种原因,我在我的笔记
我在 Grails 3 中使用 Spock 进行测试。一个特定的测试用例被破坏了,因为在这种情况下,Grails 在两个不同的 session 中与我的数据库对话。我的规范用 @Rollback 注释
我是 PostgreSQL/libpq 的新手。所以请帮我澄清我的困惑: 假设我首先执行“START TRANSACTION”并进行适当的错误检查 (PQresultStatus(res) != [p
问题 在遇到数据库完整性错误(SQLAlchemy 的要求)后调用 SQLAlchemy 的 session.rollback() 会导致所有 session 对象被释放。这包括之前选择创建的对象。
与使用 EF Core 管理交易相关的问题: IDbContextTransaction.Rollback() 是否有机会返回异常,以便将其放在 try block 中是否有意义? 即 try
在下面的代码中,当我在表中插入数据时,回滚的目的是什么,如果我想回滚,我不应该插入它,那么使用回滚的合适方法是什么? BEGIN TRANSACTION Insert into dimCustomr
我在 START TRANSACTION 和 COMMIT 之间执行了多个查询 SQL 语句。 如果出现任何问题,进程将在中间终止而不执行 COMMIT 并且数据库不会发生任何事情。到目前为止,没有执
我们可以在 Oracle 中创建保存点,然后我们可以通过调用 ROLLBACK TO SAVEPOINT 回滚到特定的保存点。 . UPDATE employees SET salary =
ROLLBACK TRAN 会回滚我的 SP 中的所有内容吗? (包括对另一个SP的调用) 示例: BEGIN TRAN INSERT INTO (table1) VALUES (1,'abc
我一直在尝试使用我们现有的 Spring JUnit 测试(通过 @TransactionalTestExecutionListener 运行或子类化 AbstractTransactionalUni
在我的托管 Bootstrap 的 bundle.wxs 中,我链接了多个包: 我正在订阅 ExecutePackageBegin/ExecutePackageComplete
此代码使用存储在 $in 中的所有查询进行交易。例如$in = "查询1;查询2;查询3;" 当其中一个失败时,它被回滚但没有效果,没有错误的查询被插入到数据库中 $conn = mysql_
我有以下代码在事务中执行数据库操作: try { //start transaction repository.startTrans
有一天,我注意到我的事务不接受 ActiveRecord::Rollback。我有一个这样的例子: 示例 ActiveRecord::Base.transaction do puts @shi
连接没问题,但是查询语句好像有问题。 query1 = """SELECT * FROM `DATABASE` WHERE `coin` = 'LTC'""" query2 = """SELECT *
我是一名优秀的程序员,十分优秀!