SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Fun with Transactions - Part IV


Fun with Transactions - Part IV

Author
Message
tejaswini.patil
tejaswini.patil
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1162 Visits: 234
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.
Nakul Vachhrajani
Nakul Vachhrajani
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3232 Visits: 2149
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
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
tejaswini.patil
tejaswini.patil
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1162 Visits: 234
Thanks for the clarification Nakul. :-)

And definitely I will do more practice on Transactions.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search