|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Monday, July 25, 2011 3:10 AM
Points: 440,
Visits: 105
|
|
I was wrong answer: "Outer transaction is still open....rolling back... ". I learned that my wrong answer can be obtained with a SAVEPOINT:
CREATE TABLE MyTable (MyId INT IDENTITY (1,1), MyCity NVARCHAR(50))
BEGIN TRANSACTION OuterTran INSERT INTO MyTable VALUES ('Boston')
BEGIN TRANSACTION InnerTran SAVE TRAN InnerTranSavePoint -- NEW CODE: SAVEPOINT INSERT INTO MyTable VALUES ('London') ROLLBACK TRAN InnerTranSavePoint -- MODIFIED CODE: RETURN TO SAVE TRAN IF (@@TRANCOUNT = 0) BEGIN PRINT 'All transactions were rolled back' END ELSE BEGIN PRINT 'Outer transaction is still open....rolling back...' ROLLBACK TRANSACTION OuterTran END
DROP TABLE MyTable
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, April 08, 2013 7:12 AM
Points: 171,
Visits: 99
|
|
| Learned something new today:)
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 6:16 AM
Points: 2,862,
Visits: 2,464
|
|
Good question - almost got me. If i hadn't have reread the code and noticed the way the rollback was written. Proves that rereading code is good.
Steve Jimmo Sr DBA “If we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 12:06 AM
Points: 3,226,
Visits: 64,101
|
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 2:32 PM
Points: 586,
Visits: 357
|
|
Always good to have a refresher on transaction basics! Thanks
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 9:58 AM
Points: 2,570,
Visits: 1,530
|
|
| Very nice question. I was unaware of savepoints for transactions either.
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 10:08 AM
Points: 87,
Visits: 121
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 11:44 AM
Points: 1,110,
Visits: 1,145
|
|
LUCAB (9/14/2010)
I was wrong answer: "Outer transaction is still open....rolling back... ". I learned that my wrong answer can be obtained with a SAVEPOINT: CREATE TABLE MyTable (MyId INT IDENTITY (1,1), MyCity NVARCHAR(50))
BEGIN TRANSACTION OuterTran INSERT INTO MyTable VALUES ('Boston')
BEGIN TRANSACTION InnerTran SAVE TRAN InnerTranSavePoint -- NEW CODE: SAVEPOINT INSERT INTO MyTable VALUES ('London') ROLLBACK TRAN InnerTranSavePoint -- MODIFIED CODE: RETURN TO SAVE TRAN IF (@@TRANCOUNT = 0) BEGIN PRINT 'All transactions were rolled back' END ELSE BEGIN PRINT 'Outer transaction is still open....rolling back...' ROLLBACK TRANSACTION OuterTran END
DROP TABLE MyTable
In the above code, the line
BEGIN TRANSACTION InnerTran Does not appear to be required (at least to effect the same result).
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, November 18, 2010 8:24 AM
Points: 12,
Visits: 85
|
|
You cannot simply rollback a named nested transaction you can only rollback to a savepoint.
"Naming multiple transactions in a series of nested transactions with a transaction name has little effect on the transaction. Only the first (outermost) transaction name is registered with the system. A rollback to any other name (other than a valid savepoint name) generates an error. None of the statements executed before the rollback is, in fact, rolled back at the time this error occurs. The statements are rolled back only when the outer transaction is rolled back".
http://msdn.microsoft.com/en-us/library/ms188929.aspx
This example uses a savepoint to rollback a nested transaction:
CREATE TABLE MyTable (MyId INT IDENTITY (1,1), MyCity NVARCHAR(50))
BEGIN TRANSACTION OuterTran INSERT INTO MyTable VALUES ('Boston')
Save Transaction SavePoint1
BEGIN TRANSACTION InnerTran INSERT INTO MyTable VALUES ('London') ROLLBACK TRAN SavePoint1 IF (@@TRANCOUNT = 0) BEGIN PRINT 'All transactions were rolled back' END ELSE BEGIN PRINT 'Outer transaction is still open....rolling back...' ROLLBACK TRANSACTION OuterTran END
DROP TABLE MyTable go
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, April 30, 2013 12:19 PM
Points: 74,
Visits: 127
|
|
try this:
CREATE TABLE MyTable (MyId INT IDENTITY (1,1), MyCity NVARCHAR(50))
BEGIN TRANSACTION OuterTran INSERT INTO MyTable VALUES ('Boston')
BEGIN TRANSACTION InnerTran INSERT INTO MyTable VALUES ('London') ROLLBACK TRAN
BEGIN TRANSACTION InnerTran INSERT INTO MyTable VALUES ('paris')
IF (@@TRANCOUNT = 0) BEGIN PRINT 'All transactions were rolled back' END ELSE BEGIN PRINT 'Outer transaction is still open....rolling back...' ROLLBACK TRANSACTION OuterTran END
Message:
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected) Outer transaction is still open....rolling back... Msg 6401, Level 16, State 1, Line 23 Cannot roll back OuterTran. No transaction or savepoint of that name was found.
It will be the else loop case.
|
|
|
|