Fun with Transactions - Part IV

  • Thanks for the question.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Thanks for the nice Question 🙂

  • thank you for the question and thanks to all the people that gived more examples to explain it because I have read the microsoft explanation but my comprehension of it was somewhat wrong

  • Good series of questions Nakul. Thanks

    Thanks

  • Perfect explanation and example .

    🙂

  • Hi,

    I am little confused.

    From the code given for Fun with Transactions - Part III, I learned that we can commit inner transaction and then rollback/ commit outer transaction.

    ----------------------------------------------------------

    Fun with Transactions - Part III code :-

    CREATE TABLE MyTable (MyId INT IDENTITY (1,1),

    MyCity NVARCHAR(50))

    BEGIN TRANSACTION OuterTran

    INSERT INTO MyTable VALUES ('Boston')

    BEGIN TRAN InnerTran

    INSERT INTO MyTable VALUES ('London')

    COMMIT TRAN InnerTran

    ROLLBACK TRANSACTION OuterTran

    SELECT * FROM MyTable

    DROP TABLE MyTable

    ----------------------------------------------------------

    Where as for Fun with Transactions - Part IV, We cannot rollback inner transaction. Instead of starting a nested transaction the outer transaction was saved to a savepoint and then rollback upto the savepoint. I feel this is not a nested transaction. There is only one transaction which is saved to a savepoint and can be commtted / rolled back upto the save point. Please correct me if I am wrong.

    After reading the earlier discussions I am still not clear on how to use nested transactions. Can any one explain?

    Thanks.

  • Hello, Tejaswini!

    It's okay to be confused. Not a problem. I appreciate the fact that you asked for more clarification.

    The point illustrated by #3 is, in itself the answer to #4.

    Part #3 of this series illustrated that a rollback command will rollback the transactions all the way to the outermost transaction. However, this does not mean that one cannot have logical "save points" that allow you to rollback to the most recent/outermost save point, provided the rollback is a conditional rollback. Without a save point, the rollback must go all the way to the outermost transaction - with named transactions, you cannot rollback to an inner transaction without a save point. That is the point illustrated by part #4.

    Hence, the following would have worked with respect to Part #4

    CREATE TABLE MyTable (MyId INT IDENTITY (1,1),

    MyCity NVARCHAR(50))

    BEGIN TRANSACTION OuterTran

    INSERT INTO MyTable VALUES ('Boston')

    BEGIN TRAN InnerTran

    INSERT INTO MyTable VALUES ('London')

    SAVE TRAN InnerTran

    INSERT INTO MyTable VALUES ('Paris')

    SELECT *,'Post Save' FROM MyTable

    ROLLBACK TRAN InnerTran

    SELECT *,'Post Rollback Inner Transaction' FROM MyTable

    ROLLBACK TRANSACTION OuterTran

    SELECT *, 'Post Rollback Outer Transaction' FROM MyTable

    DROP TABLE MyTable

    To get more clarity, what I would recommend is to play around with transactions for a while - there can be no better teacher than experience.

    Thanks & Regards,
    Nakul Vachhrajani.
    http://nakulvachhrajani.com

    Follow me on
    Twitter: @sqltwins

  • Thanks for the clarification Nakul. 🙂

    And definitely I will do more practice on Transactions.

Viewing 8 posts - 16 through 22 (of 22 total)

You must be logged in to reply to this topic. Login to reply