Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6948499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:49:52+00:00 2026-05-27T13:49:52+00:00

I have two procedure: create procedure P2 as begin print @@trancount begin tran if

  • 0

I have two procedure:

create procedure P2
as
begin
    print @@trancount
    begin tran

    if 1 = 1
    begin
          print @@trancount
          rollback
    end
    else
    begin
          commit
    end
end
go


create procedure P1
as
begin
    begin tran
        print @@trancount
        exec P2
        print @@trancount
    commit
end
go

exec P1

When I call P1 I got:

1
1
2
Msg 266, Level 16, State 2, Procedure P2, Line 0
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0.
0
Msg 3902, Level 16, State 1, Procedure P1, Line 8
The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.

I expected result like this:

1
1
2
1

My questions are:

1. Why do I got this error?

2. How should I write my procedure to do it good?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:49:52+00:00Added an answer on May 27, 2026 at 1:49 pm

    When your Procedure P2 executes the rollback line, you are rolling back the outer-most transaction. (The one originally created in P1) This changes the transaction count from the time before P2 was called, until after it was executed.

    If you expect a procedure to affect the transaction count, you could call the procedure in a Try-Catch to be able to handle the informational message you get back.

    From MSDN:

    In stored procedures, ROLLBACK TRANSACTION statements without a savepoint_name
    or transaction_name roll back all statements to the outermost BEGIN TRANSACTION.
    A ROLLBACK TRANSACTION statement in a stored procedure that causes @@TRANCOUNT
    to have a different value when the stored procedure completes than the
    @@TRANCOUNT value when the stored procedure was called produces an informational
    message. This message does not affect subsequent processing.
    

    You may also want to look at the article on nesting transactions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two PL/SQL Stored procedure each handling its own Transaction (Begin/Commit and Rollback
I have this two functions: procedure TDisplay.CubAssign(VAR Obj: TCubObj; CONST bReleaseOnExit: boolean); begin ReleaseCubOnExit:=
I have the following SP: CREATE PROCEDURE [dbo].[sp_LockReader] AS BEGIN SET NOCOUNT ON; begin
I have created a procedure create procedure testProcedure_One as DECLARE @Query nvarchar(4000) begin SET
i have following two queries, one is create procedure query and other is the
I have written below stored procedure. In which i need to create Two Temporary
I have two cursors in my procedure that only differ on the table name
I have two stored procedures I wish to use in my stored procedure, but
I have a stored procedure that returns two int Values. The first can not
I have a stored procedure that takes no parameters, and it returns two fields.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.