Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Mismatch # of begin/commit statments, help please.

Author  Topic 

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2012-01-25 : 00:04:31
I am new to running transactions and I am getting the error "Transaction count after execute indeicates a mismatching number of begin and commit statements. Previous count = 1, current count = 0". Can anyone look at the below code and tell me what might be wrong? I am calling Proc 1 which calls Proc 2

Proc 1*
ALTER proc [dbo].[spRaces]
@UpdateType as char(6),
@RaceID as int = null,
@SectionID as smallint = null,
@Race as varchar(50) = null,
@RaceDescription as varchar(MAX) = null
as
set nocount on
set xact_abort on
set arithabort on
declare @ErrorNum int

begin transaction

--Process a new race
if (@UpdateType = 'Insert')
begin
insert into dbo.Races (SectionID, Race, RaceDescription) values (@SectionID, @Race, @RaceDescription);

select @ErrorNum = @@ERROR;

if (@ErrorNum = 0)
begin
exec @ErrorNum = dbo.spSubMenuItems @SectionID, 'Information', 'Races', 'Races', 'Races';
end
end
else
--Process an updated race
if (@UpdateType = 'Update')
begin
update dbo.Races
set SectionID = coalesce(@SectionID, SectionID), Race = coalesce(@Race, Race), RaceDescription = coalesce(@RaceDescription, RaceDescription)
where ID = @RaceID;

select @ErrorNum = @@ERROR;
end
else
--Process a race removal
if (@UpdateType = 'Delete')
begin
delete dbo.Races where ID = @RaceID;
select @ErrorNum = @@ERROR;
end

if (@ErrorNum = 0)
begin
insert into dbo.SiteUpdates (SectionIDUpdated, SubSectionName, UpdatedItemName, UpdateType)
values (@SectionID, 'Races', @Race, @UpdateType);

select @ErrorNum = @@ERROR;
end
if (@ErrorNum = 0)
begin
commit
end
else
begin
rollback
end

return @ErrorNum;


proc 2:
ALTER proc [dbo].[spSubMenuItems]
@SectionID as smallint,
@MenuTitle as char(11),
@FileName as varchar(50),
@LinkTitle as varchar(50),
@LinkName as varchar(50)
as
set nocount on
set xact_abort on
set arithabort on
declare @ErrorNum int

begin transaction

if not exists (select 1 from dbo.SectionMenu where SectionID = @SectionID and MenuTitle = @MenuTitle)
begin
insert into dbo.SectionMenu (SectionID, MenuTitle, FileName, LinkTitle, LinkName)
values (@SectionID, @MenuTitle, @FileName, @LinkTitle, @LinkName);

set @ErrorNum = @@Error;
end

if (@ErrorNum = 0)
begin
commit
end
else
begin
rollback
end

return @ErrorNum


--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

TimS
Posting Yak Master

198 Posts

Posted - 2012-01-25 : 17:11:43
Is not the word "transaction" still required after the keywords "commit" and "rollback"?

Tim S.

PS: Been several years since I last did T-SQL
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2012-01-25 : 17:34:42
"Is not the word "transaction" still required after the keywords "commit" and "rollback"?"

No, COMMIT / ROLLBACK on its own is fine.

"Previous count = 1, current count = 0"

I expect the Inner Sproc has done a ROLLBACK, which will have rolled-back the Outer Sproc's transaction too.

If you only want to rollback your "own" transaction (i.e. the inner Sproc only rolls back the transaction that it created) you'll need to use a SAVEPOINT and rollback to that SAVEPOINT. (In practice its a bit more complicated than that, but that's the gist of it)
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-01-25 : 18:30:47
Here's an old thread which you can probably pick up a nice feel for nested transactions:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=141905

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2012-01-25 : 19:02:10
That was the tread I was thinking of, but I was too lazy to go SEARCH for it - thanks T.C.
Go to Top of Page
   

- Advertisement -