﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Nakul Vachhrajani  / Fun with Transactions  - Part I / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 18 May 2013 19:55:42 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>[quote][b]da-zero (9/14/2010)[/b][hr][quote]Nesting does work with transactions. It's just that if you use rollback transaction, then all open transactions are rolled back. If you commit a transaction, then only the innermost transaction is committed.[/quote]No, you can't commit an inner transaction - what happens when you issue a commit command is that the transaction count gets decremented, and if the result is zero, the transaction (which is the outermost transaction) is committed (and the data modifications affected by this commit include all made by any so-called nested transactions); if the result is non-zero, nothing is committed.  Neither can you roll back a nested transaction - you can only roll back the whole nest (including the outermost transaction).</description><pubDate>Sat, 18 Sep 2010 11:40:13 GMT</pubDate><dc:creator>L' Eomot Inversé</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>[quote][b]Nakul Vachhrajani (9/15/2010)[/b][hr]Hey, Mark!That's really good to know!Coincidence is a great phenomenon - I wonder how they happen?Have a wonderful rest-of-the-week ahead![/quote]For me,around here this happens about once every 3 months.  But usually I learn about it 2-3 weeks before I need to use it.. not 24 hours :)Tally Tables, Dynamic Paramterized Queries and several other topics typically hit here about 3 weeks before I need them.  This time it was in a QOTD.  As I said, awesome.</description><pubDate>Wed, 15 Sep 2010 09:07:02 GMT</pubDate><dc:creator>mtassin</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>[quote][b]mtassin (9/15/2010)[/b][hr]I don't even know how excitingly appropriate this is.Yesterday I learned about savepoints.  Today a trigger that was invoking a stored procedure that created an inner transaction and rolled it back if a test case was pushed into production.Nobody could figure out why the table with the tirgger was never getting a record.Stuck in a transaction save point right after the BEGIN TRANSACTION in the stored proc and set the ROLLBACK to rollback to the savepoint and made the problem go away.Awesome[/quote]Hey, Mark!That's really good to know!Coincidence is a great phenomenon - I wonder how they happen?Have a wonderful rest-of-the-week ahead!</description><pubDate>Wed, 15 Sep 2010 08:40:34 GMT</pubDate><dc:creator>Nakul Vachhrajani</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>I don't even know how excitingly appropriate this is.Yesterday I learned about savepoints.  Today a trigger that was invoking a stored procedure that created an inner transaction and rolled it back if a test case was pushed into production.Nobody could figure out why the table with the tirgger was never getting a record.Stuck in a transaction save point right after the BEGIN TRANSACTION in the stored proc and set the ROLLBACK to rollback to the savepoint and made the problem go away.Awesome</description><pubDate>Wed, 15 Sep 2010 08:37:59 GMT</pubDate><dc:creator>mtassin</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>QoTD keeping us on our toes.  Nice question, thanks.</description><pubDate>Wed, 15 Sep 2010 07:20:18 GMT</pubDate><dc:creator>Daniel Bowlin</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>Nice question and discussion, thanks!</description><pubDate>Tue, 14 Sep 2010 23:29:59 GMT</pubDate><dc:creator>UMG Developer</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>Good question, thanks.</description><pubDate>Tue, 14 Sep 2010 12:17:17 GMT</pubDate><dc:creator>Cliff Jones</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>try this: CREATE TABLE MyTable (MyId INT IDENTITY (1,1),                      MyCity NVARCHAR(50))BEGIN TRANSACTION OuterTranINSERT INTO MyTable VALUES ('Boston')BEGIN TRANSACTION InnerTranINSERT INTO MyTable VALUES ('London')ROLLBACK TRANBEGIN TRANSACTION InnerTranINSERT 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 ENDMessage:(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 23Cannot roll back OuterTran. No transaction or savepoint of that name was found.It will be the else loop case.</description><pubDate>Tue, 14 Sep 2010 11:09:01 GMT</pubDate><dc:creator>Rahul26</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>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.aspxThis example uses a savepoint to rollback a nested transaction:CREATE TABLE MyTable (MyId INT IDENTITY (1,1),                      MyCity NVARCHAR(50))BEGIN TRANSACTION OuterTranINSERT INTO MyTable VALUES ('Boston')Save Transaction SavePoint1BEGIN TRANSACTION InnerTranINSERT 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 ENDDROP TABLE MyTablego</description><pubDate>Tue, 14 Sep 2010 10:27:22 GMT</pubDate><dc:creator>richard.maw</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>[quote][b]LUCAB (9/14/2010)[/b][hr]I was wrong answer:  "Outer transaction is still open....rolling back... ".I learned that my wrong answer can be obtained with a SAVEPOINT:[code="sql"]CREATE TABLE MyTable (MyId INT IDENTITY (1,1),                      MyCity NVARCHAR(50))BEGIN TRANSACTION OuterTranINSERT INTO MyTable VALUES ('Boston')BEGIN TRANSACTION InnerTranSAVE TRAN InnerTranSavePoint    -- NEW CODE: SAVEPOINTINSERT 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 ENDDROP TABLE MyTable[/code][/quote]In the above code, the line [code="sql"]BEGIN TRANSACTION InnerTran[/code]Does not appear to be required (at least to effect the same result).</description><pubDate>Tue, 14 Sep 2010 09:15:28 GMT</pubDate><dc:creator>marklegosz</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>A pleasant question.</description><pubDate>Tue, 14 Sep 2010 09:14:41 GMT</pubDate><dc:creator>Lana Gold</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>Very nice question. I was unaware of savepoints for transactions either.</description><pubDate>Tue, 14 Sep 2010 08:32:41 GMT</pubDate><dc:creator>KWymore</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>Always good to have a refresher on transaction basics!  Thanks</description><pubDate>Tue, 14 Sep 2010 08:30:18 GMT</pubDate><dc:creator>daveb87</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>[quote][b]LUCAB (9/14/2010)[/b][hr]I was wrong answer:  "Outer transaction is still open....rolling back... ".I learned that my wrong answer can be obtained with a SAVEPOINT:[code="sql"]SAVE TRAN InnerTranSavePoint    -- NEW CODE: SAVEPOINT[/code][/quote]And while the actual question didn't teach me anything new, I hadn't heard of Transaction savepoints... that's really neat.</description><pubDate>Tue, 14 Sep 2010 07:04:43 GMT</pubDate><dc:creator>mtassin</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>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.</description><pubDate>Tue, 14 Sep 2010 06:25:50 GMT</pubDate><dc:creator>sjimmo</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>Learned something new today:)</description><pubDate>Tue, 14 Sep 2010 05:27:10 GMT</pubDate><dc:creator>larsts</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>I was wrong answer:  "Outer transaction is still open....rolling back... ".I learned that my wrong answer can be obtained with a SAVEPOINT:[code="sql"]CREATE TABLE MyTable (MyId INT IDENTITY (1,1),                      MyCity NVARCHAR(50))BEGIN TRANSACTION OuterTranINSERT INTO MyTable VALUES ('Boston')BEGIN TRANSACTION InnerTranSAVE TRAN InnerTranSavePoint    -- NEW CODE: SAVEPOINTINSERT 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 ENDDROP TABLE MyTable[/code]</description><pubDate>Tue, 14 Sep 2010 01:59:25 GMT</pubDate><dc:creator>LUCAB</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>That'll teach me to shoot before I aim.Thanks</description><pubDate>Tue, 14 Sep 2010 01:57:48 GMT</pubDate><dc:creator>phil.wood 94423</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>[quote][b]phil.wood 94423 (9/14/2010)[/b][hr]Oops, I got it wrong.In my defence, nesting works all over Microsoft so I'm a little surprised they never got it working here.  Almost seems like an oversight.Would have made more sense if they had employed a slightly different keyword such as e.g.ROLLBACK TRAN ALLorROLLBACK TRANSACTIONSJust my view :-)[/quote]Nesting does work with transactions. It's just that if you use rollback transaction, then all open transactions are rolled back. If you commit a transaction, then only the innermost transaction is committed.If you really want full nesting, you can use checkpointing in your transactions and use named transactions.</description><pubDate>Tue, 14 Sep 2010 01:52:42 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>Oops, I got it wrong.In my defence, nesting works all over Microsoft so I'm a little surprised they never got it working here.  Almost seems like an oversight.Would have made more sense if they had employed a slightly different keyword such as e.g.ROLLBACK TRAN ALLorROLLBACK TRANSACTIONSJust my view :-)</description><pubDate>Tue, 14 Sep 2010 01:46:55 GMT</pubDate><dc:creator>phil.wood 94423</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>[quote][b]da-zero (9/14/2010)[/b][hr]Nice question about the transaction basics. As I'm reading the self-paced training kit for the database developper certification, this was a really easy one for me, as it is explained quite clearly in the book.[/quote]It is really good to know that the question helped in your preparations. Best of luck for your exam!</description><pubDate>Tue, 14 Sep 2010 01:14:25 GMT</pubDate><dc:creator>Nakul Vachhrajani</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>Nice question about the transaction basics. As I'm reading the self-paced training kit for the database developper certification, this was a really easy one for me, as it is explained quite clearly in the book.</description><pubDate>Tue, 14 Sep 2010 00:57:49 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>My guess is right. :-)Nice Question.</description><pubDate>Tue, 14 Sep 2010 00:08:05 GMT</pubDate><dc:creator>rals</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>Good Question thanks :)</description><pubDate>Mon, 13 Sep 2010 23:55:14 GMT</pubDate><dc:creator>deepak.a</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>learned something new today... thanks...:-)</description><pubDate>Mon, 13 Sep 2010 22:41:52 GMT</pubDate><dc:creator>ziangij</dc:creator></item><item><title>RE: Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>Nice question - thanks.</description><pubDate>Mon, 13 Sep 2010 21:32:01 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>Fun with Transactions  - Part I</title><link>http://www.sqlservercentral.com/Forums/Topic985201-2785-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/transactions/70996/"&gt;Fun with Transactions  - Part I&lt;/A&gt;[/B]</description><pubDate>Mon, 13 Sep 2010 21:31:46 GMT</pubDate><dc:creator>Nakul Vachhrajani</dc:creator></item></channel></rss>