[英]Msg 3902, Level 16, State 1. The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION
DECLARE @cnt_inv int,
@cnt_jrn int,
@pl_per varchar(2),
@pl_yr varchar(4),
@pl_jrn varchar (6),
@pl_inv varchar (6)
IF @@ERROR <> 0
BEGIN
BEGIN TRANSACTION JD_Imp
IF @cnt_inv > 0
BEGIN
BEGIN TRANSACTION JD_Inv
COMMIT TRANSACTION JD_Inv;
PRINT N'The Invoice Commits DONE.';
END
IF @cnt_jrn > 0
BEGIN
BEGIN TRANSACTION JD_Jrn
COMMIT TRANSACTION JD_Jrn;
PRINT N'The Journals Commits DONE.';
END
COMMIT TRANSACTION JD_Imp;
END
The core of your issue is this: 您的问题的核心是:
IF @cnt_jrn > 0
BEGIN TRANSACTION JD_Jrn
All this will do is only start a new transaction if @cnt_jrn > 0
. 如果
@cnt_jrn > 0
这样做只会启动一个新事务。 It's still going to execute all of the code below regardless of the condition. 无论条件如何,它仍然会执行下面的所有代码。 So if
@cnt_jrn <= 0
, it's going to call commit transaction JD_Jrn
without ever having started it. 因此,如果
@cnt_jrn <= 0
,它将调用commit transaction JD_Jrn
而不启动它。
You need to enclose the body of any multi-statement if
body with begin
and end
. if
具有begin
和end
的主体,则需要将任何多语句的主体括起来。 For example: 例如:
IF @cnt_jrn > 0
BEGIN
BEGIN TRANSACTION JD_Jrn
... code ...
END
But you are enclosing single insert
and update
statements in transactions, which is not necessary. 但是您在事务中包含单个
insert
和update
语句,这是不必要的。 SQL operations are guaranteed to be atomic, so you only need a transaction if you're spanning multiple operations. SQL操作保证是原子操作,因此如果您跨越多个操作,则只需要一个事务。
Problem solved..... 问题解决了.....
(a) Having the Begin...END Blocks did solve the error message Msg 3902.. I noticed that without the BEGIN..END Blocks, previous runs of the procedure will still be hanging uncommitted (a)让Begin ... END Blocks确实解决了错误消息Msg 3902 ..我注意到没有BEGIN..END块,以前的程序运行仍然是未提交的
(b) IF @@ERROR <> 0 BEGIN will always be true so no wonder the script block inside was not being executed. (b)IF @@ ERROR <> 0 BEGIN将始终为true,因此难怪内部的脚本块未被执行。
(c) The Debugger was not moving past the IF statement due to (b). (c)由于(b),调试器没有超过IF语句。
Thanks a lot Adam. 非常感谢Adam。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.