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 7657287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:59:31+00:00 2026-05-31T12:59:31+00:00

I have a stored procedure that needs to set a save point so that

  • 0

I have a stored procedure that needs to set a save point so that it can, under certain circumstances, undo everything it did and return an error code to the caller, or accept/commit it and return success to the caller. But I need it to work whether the caller has already started a transaction or not. The doc is extremely confusing on this subject. Here is what I think will work, but I’m not certain of all the ramifications.

The thing is – this Stored Procedure (SP) is called by others. So I don’t know if they’ve started a transaction or not… Even if I require users to start a transaction to use my SP, I still have questions about the proper use of Save Points …

My SP will test if a transaction is in progress, and if not, start one with BEGIN TRANSACTION. If a transaction is already in progress, it will instead create a save point with SAVE TRANSACTION MySavePointName, and save the fact this is what I did.

Then if I have to roll back my changes, if I did a BEGIN TRANSACTION earlier, then I will ROLLBACK TRANSACTION. If I did the save point, then I will ROLLBACK TRANSACTION MySavePointName. This scenario seems to work great.

Here is where I get a little confused – if I want to keep the work I’ve done, if I started a transaction I will execute COMMIT TRANSACTION. But if I created a save point? I tried COMMIT TRANSACTION MySavePointName, but then the caller tries to commit its transaction and gets an error:

The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.

So I’m wondering then – a save point can be rolled back (that works: ROLLBACK TRANSACTION MySavePointName will NOT roll back the caller’s transaction). But perhaps one never needs to “commit” it? It just stays there, in case you need to roll back to it, but goes away once the original transaction is committed (or rolled back)?

If there is a “better” way to “nest” a transaction, please shed some light as well. I haven’t figured out how to nest with BEGIN TRANSACTION but only rollback or commit my internal transaction. Seems ROLLBACK will always roll back to the top transaction, while COMMIT simply decrements @@trancount.

  • 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-31T12:59:33+00:00Added an answer on May 31, 2026 at 12:59 pm

    I believe I’ve figured this all out now, so I will answer my own question…

    I’ve even blogged my findings if you want more details at http://geekswithblogs.net/bbiales/archive/2012/03/15/how-to-nest-transactions-nicely—quotbegin-transactionquot-vs-quotsave.aspx

    So my SP starts with something like this, to start a new transaction if there is none, but use a Save Point if one is already in progress:

    DECLARE @startingTranCount int
    SET @startingTranCount = @@TRANCOUNT
    
    IF @startingTranCount > 0
        SAVE TRANSACTION mySavePointName
    ELSE
        BEGIN TRANSACTION
    -- …
    

    Then, when ready to commit the changes, you only need to commit if we started the transaction ourselves:

    IF @startingTranCount = 0
        COMMIT TRANSACTION
    

    And finally, to roll back just your changes so far:

    -- Roll back changes...
    IF @startingTranCount > 0
        ROLLBACK TRANSACTION MySavePointName
    ELSE
        ROLLBACK TRANSACTION
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that needs to pass a result set from another
I have a stored procedure that needs to convert hexadecimal numbers to their decimal
I'm writing a stored procedure that needs to have a lot of conditioning in
I have a stored procedure that returns multiple tables. How can I execute and
I have a stored procedure that returns two int Values. The first can not
I have a temp table that needs the values of a Stored procedure. So
I have a stored procedure that uses sp_executesql to generate a result set, the
I have a strange, sporadic issue. I have stored procedure that returns back 5
I have a stored procedure that consists of a single select query used to
I have a stored procedure that looks like: CREATE PROCEDURE dbo.usp_TestFilter @AdditionalFilter BIT =

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.