|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, April 19, 2011 1:59 AM
Points: 1,042,
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.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 9:52 AM
Points: 1,277,
Visits: 1,608
|
|
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://beyondrelational.com/modules/2/blogs/77/nakuls-blog.aspx Be courteous. Drive responsibly.
Follow me on Twitter: @nakulv_sql Google Plus: +Nakul
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, April 19, 2011 1:59 AM
Points: 1,042,
Visits: 234
|
|
Thanks for the clarification Nakul. 
And definitely I will do more practice on Transactions.
|
|
|
|