Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm getting this error

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


What I have tried:

create PROCEDURE [dbo].[sp]
	@Id1 int
	,@BBId int
        ,@Text nvarchar(100) =''
AS
Begin
  
	Begin transaction
	        declare @Id int
            
		INSERT INTO table1 
		select @Text where @Text <> ''

		SET @Id = SCOPE_IDENTITY()

		INSERT INTO table2 (Id1, @Id)
			select @Id1,@Id from table3
			where table3.BBId = @BBId AND table3.Flag= 0 and 
                        table3.Id1 = @Id1
		if(@@rowcount>0)
		begin
		        UPDATE    table3
			SET       Flag= 1
			WHERE   
				 table3.BBId = @BBID 
				AND table3.Id1 = @Id1
	       COMMIT TRANSACTION
        end
        else
        rollback transaction
  
END
Return 0
Posted
Updated 7-Oct-20 2:32am

Without your data and being able to run your SP against it, there isn't anything we can do: the best guess is that something in there is causing an exception, and since you don't use a TRY ... CATCH block at all neither the ROLLBACK nor COMMIT are being executed.

Try adding error handling and see if it improves.
 
Share this answer
 
Comments
Member 14800672 5-Oct-20 3:17am    
Hi OriginalGriff,

this error happens, when it reaches else ( i mean if @@rowCount is less than 0)
I tried adding try and catch in code below but still same error. Any idea?

create PROCEDURE [dbo].[sp]
@Id1 int
,@BBId int
,@Text nvarchar(100) =''
AS
Begin
begin try

Begin transaction
declare @Id int

INSERT INTO table1
select @Text where @Text <> ''

SET @Id = SCOPE_IDENTITY()

INSERT INTO table2 (Id1, @Id)
select @Id1,@Id from table3
where table3.BBId = @BBId AND table3.Flag= 0 and
table3.Id1 = @Id1
if(@@rowcount>0)
begin
UPDATE table3
SET Flag= 1
WHERE
table3.BBId = @BBID
AND table3.Id1 = @Id1
COMMIT TRANSACTION
end
else
rollback transaction
end try
begin catch
SELECT ERROR_MESSAGE()

ROLLBACK

end catch

END
Return 0
You need to reverse the IF statement and commit at the end of the transaction.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900