EXECUTE 之后的事务计数表明 BEGIN 和 COMMIT 的数量不匹配?
Posted
技术标签:
【中文标题】EXECUTE 之后的事务计数表明 BEGIN 和 COMMIT 的数量不匹配?【英文标题】:Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT? 【发布时间】:2012-08-10 15:39:52 【问题描述】:问题:我有这个 SQL 脚本:
DECLARE @in_TE_UID varchar(36)
DECLARE @in_ZO_BETE_Sort int
DECLARE @in_user int
SET @in_TE_UID = '9f510440-8828-44ce-bbea-6bc866902262'
SET @in_ZO_BETE_Sort = 0
SET @in_user = 12435
-- http://***.com/questions/884334/tsql-try-catch-transaction-in-trigger
BEGIN TRANSACTION BeforeUpdate;
BEGIN TRY
IF NOT EXISTS
(
SELECT 1 FROM T_FMS_ZO_Benutzer_TeaserOrder
WHERE (T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_Status = 1)
AND (T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_BE_ID = @in_user)
AND
(
T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_TE_UID = @in_TE_UID
OR
(
@in_TE_UID IS NULL
AND
T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_TE_UID IS NULL
)
)
)
BEGIN
INSERT INTO T_FMS_ZO_Benutzer_TeaserOrder
(
ZO_BETE_UID
,ZO_BETE_BE_ID
,ZO_BETE_TE_UID
,ZO_BETE_Sort
,ZO_BETE_Status
)
VALUES
(
NEWID() --<ZO_BETE_UID, uniqueidentifier,>
,@in_user --<ZO_BETE_BE_ID, int,>
,@in_TE_UID --<ZO_BETE_TE_UID, uniqueidentifier,>
,@in_ZO_BETE_Sort --<ZO_BETE_Sort, int,>
,1 --<ZO_BETE_Status, int,>
)
;
END
ELSE
BEGIN
UPDATE T_FMS_ZO_Benutzer_TeaserOrder
SET ZO_BETE_Sort = @in_ZO_BETE_Sort
WHERE (T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_Status = 1)
AND (T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_BE_ID = @in_user)
AND
(
T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_TE_UID = @in_TE_UID
OR
(
@in_TE_UID IS NULL
AND
T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_TE_UID IS NULL
)
)
;
END
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION BeforeUpdate --RollBack in case of Error
--RETURN
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
-- Use RAISERROR inside the CATCH block to return error
-- information about the original error that caused
-- execution to jump to the CATCH block.
RAISERROR (@ErrorMessage, -- Message text.
@ErrorSeverity, -- Severity.
@ErrorState -- State.
);
END CATCH
当我运行一次时它运行良好。
如果之后我运行一个
SELECT * FROM T_FMS_ZO_Benutzer_TeaserOrder
然后超时。
如果我添加with (nolock)
那么它可以工作。
如果我查看 SSMS 中的进程,我会看到带有 LCK_M_S
的锁定选择。
如果我在代码中执行相同的命令,我总是会得到这个错误:
EXECUTE 之后的事务计数表明 BEGIN 和 COMMIT 语句的数量不匹配。先前计数 = 0,当前计数 = 1
真的,这段代码有什么问题?
【问题讨论】:
【参考方案1】:您还没有在 SQL 中的任何地方添加COMMIT
。大概它应该在END TRY
之前出现
【讨论】:
哎呀,WTF,我怎么能监督...我想我该睡觉了:)【参考方案2】:-- =============================================
-- Author: <Ashish Jaishwal>
-- Create date: <14 Jan 2016>
-- Description: <create a bulk registeration of student and Login and alloted Batch>
-- =============================================
alter proc [dbo].[SPC_Bulk_Student_Register]
(
@BatchName varchar(50),
@CourseID int,
@START_DATE Datetime,
@END_DATE Datetime,
@MAX_LIMIT int,
@EmpID int,
@ExistBatchFlag int
)
as
Begin
DECLARE @STUDENT_ID VARCHAR(15)
DECLARE @BATCHID INT
DECLARE @COURSE_ID VARCHAR(6)
--DECLARE @EMPID INT
--Note: 0 Batch is not Exist and 1 Batch is Exist
--insert into TempExcelData select * from @BulkData;
IF @ExistBatchFlag =0
BEGIN
exec tc_insert_batch @BatchName,@CourseID,@START_DATE,@END_DATE,@MAX_LIMIT,@EmpID
set @BATCHID=(select batch_id from BATCH_MASTER where batch_name=@BatchName)
END
ELSE
BEGIN
set @BATCHID=(select batch_id from BATCH_MASTER where batch_name=@BatchName)
END
--BEGIN try
BEGIN TRANSACTION
WHILE EXISTS(select top 1 ID,FNAME,LNAME,DOB,EMAILID from TempExcelData)
BEGIN
DECLARE @id INT
DECLARE @FNAME VARCHAR(40)
DECLARE @LNAME VARCHAR(40)
DECLARE @DOB DATETIME
DECLARE @EMAILID VARCHAR(40)
DECLARE @STUDENTLOGIN VARCHAR(50)
SELECT TOP 1 @id = Id,@FNAME=Fname,@LNAME =Lname,@DOB=DOB,@EMAILID=EmailID FROM TempExcelData
--HERE REGISTER THE STUDENT AND RETURN THE STUDENT ID
EXEC Spc_excel_insert_student_register @FNAME,@LNAME,@DOB,@EMAILID,@STUDENT_ID out
EXEC Spc_Excel_LoginName_Generation @FNAME,@LNAME,@STUDENTLOGIN OUT
EXEC sp_InsertStudentLogonDetails @STUDENT_ID,@STUDENTLOGIN,'welcome123','Y'
--HERE ALLOTED TO BATCH AND COURSE
EXEC tc_insertStudentRegister4_new @STUDENT_ID,@BATCHID,'',@CourseID
delete TempExcelData where id=@id
-- COMMIT TRAN
END
--COMMIT TRAN
IF ( @@error <> 0 )
BEGIN
BEGIN
ROLLBACK TRAN
END
END
IF (@@trancount > 0)
BEGIN
BEGIN
COMMIT TRAN
END
END
end
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 2, current count = 0.
I AM FACING THIS SUCH TYPE PROBLEM................!!!!
【讨论】:
以上是关于EXECUTE 之后的事务计数表明 BEGIN 和 COMMIT 的数量不匹配?的主要内容,如果未能解决你的问题,请参考以下文章
EXECUTE 之后的事务计数表明 BEGIN 和 COMMIT 语句的数量不匹配。先前计数 = 1,当前计数 = 0