简体   繁体   中英

Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT?

Question: I have this SQL script:

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://stackoverflow.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

It runs fine when I run it once.

If afterwards I run a

SELECT * FROM T_FMS_ZO_Benutzer_TeaserOrder

then it timeouts.

If I add with (nolock) then it works.

If I look at the processes in SSMS, I see a locked select with LCK_M_S .

If I execute the same command in code, I always get this error:

Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1

So really, what's wrong with this code ?

You haven't put a COMMIT in your SQL anywhere. Presumably it should go just before the END TRY

-- =============================================              
-- 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................!!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM