Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««1234»»»

Triggers 1 Expand / Collapse
Author
Message
Posted Friday, April 19, 2013 1:55 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 7:19 AM
Points: 2,814, Visits: 3,851
Hi,

There is one subtle issue with the explanation (and also the MS documentation).
It is not the ROLLBACK that is causing the abort of the batch. Instead, the batch is aborted if there is no transaction alive after the end of the trigger (at least that's what I can see from my tests).

For example, adding a BEGIN TRAN right after the ROLLBACK TRAN ensures that the batch is not aborted.


Best Regards,
Chris Büttner
Post #1444262
Posted Friday, April 19, 2013 2:04 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 5:16 AM
Points: 905, Visits: 553
Thanks for your reply Hugo.

Hugo Kornelis (4/19/2013)
By the way, it's very easy to check this kind of questions for yourself. Just copy the code in SSMS, make changes, and see what happens. (Don't do this on a production database, or even on a production instance!)


My intention of posting these kind of questions here is to make other beginners to learn few topics related to it as well as I can get various suggestions from many experts..


--
Dineshbabu
Desire to learn new things..
Post #1444263
Posted Friday, April 19, 2013 2:10 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 12:14 AM
Points: 1,870, Visits: 2,276
Hugo Kornelis (4/19/2013)
kapil_kk (4/18/2013)
oopes,

I think I have misunderstood the way to execute the given query...

Oohhh, I am so sorry that the comments in the code made you misunderstand the question. I added them to be able to refer to them in the explanation, not for any other reason, and I'm sorry if that caused people to come to wrong conclusions.
The next question in this series is scheduled for next week. Since it's already scheduled for publication, I cannot change it, or even see it anymore. That's how this site works. If my memory is not playing tricks on me, that question, too, will have similar comments. So please don't fall for that again!


Thanks Hugo, question was good and I have gained something from this question that how a rollback from a trigger affects a batch...
For the next question I will keep comments things in my mind



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1444266
Posted Friday, April 19, 2013 2:18 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:59 PM
Points: 5,797, Visits: 8,017
Christian Buettner-167247 (4/19/2013)
Hi,

There is one subtle issue with the explanation (and also the MS documentation).
It is not the ROLLBACK that is causing the abort of the batch. Instead, the batch is aborted if there is no transaction alive after the end of the trigger (at least that's what I can see from my tests).

For example, adding a BEGIN TRAN right after the ROLLBACK TRAN ensures that the batch is not aborted.

Yes, you are correct. The batch is aborted because no transaction was open when the trigger finished executing. If you change the ROLLBACK in the trigger to COMMIT (though why on earth you'd want to???), the batch still aborts.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1444268
Posted Friday, April 19, 2013 2:36 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 9:37 AM
Points: 2,729, Visits: 2,648
kapil_kk (4/18/2013)
oopes,

I think I have misunderstood the way to execute the given query...
After seeing comments First Attempts, second, third I executed one by one those insert query and forget about the GO batch separator... and I was thinking it was so easy question and count will give 3 but i was wrong and lost the points...
I think most of the think count value as 3 and executed in same fashion that I executed that's why load towards incorrect answer is more than correct answer...


Result of 3 will be given, if GO exists after this insert
-- Insert attempt #2
INSERT INTO dbo.Test (PrimKey, ValueCol)
VALUES (2, -2);
GO

Regards
IgorMi
Post #1444269
Posted Friday, April 19, 2013 4:51 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: 2 days ago @ 12:16 AM
Points: 8,289, Visits: 8,742
Good question, and an excellent explanation apart from the point raised by Christian. I don't think the comments are misleading, it is pretty common for a comment to be used simply to label a piece of code (as here) and also pretty common for a comment to say what a piece of code will do if it is executed but not say whether it will be executed or not.

There is one issue with the question, albeit an utterly trivial one: it depends on the setting of IMPLICIT_TRANSACTIONS for the connection. If that's set ON, the rollback includes rolling back the two CREATE statements and the select will return not 1 but an error. Of course the default is OFF, but it would be preferable to mention that the OFF setting is assumed. I'll admit that I would almost certainly have forgotten to mention that myself if I had produced this question, though, and I don't consider leaving it out an omission that in any way spoils the question.


Tom
Post #1444304
Posted Friday, April 19, 2013 4:55 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:59 PM
Points: 5,797, Visits: 8,017
L' Eomot Inversé (4/19/2013)
There is one issue with the question, albeit an utterly trivial one: it depends on the setting of IMPLICIT_TRANSACTIONS for the connection. If that's set ON, the rollback includes rolling back the two CREATE statements and the select will return not 1 but an error. Of course the default is OFF, but it would be preferable to mention that the OFF setting is assumed.

Ahh, yes. Good catch, Tom! I should have thought of that when I created the question.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1444306
Posted Friday, April 19, 2013 5:13 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 12:14 AM
Points: 1,870, Visits: 2,276
IgorMi (4/19/2013)
kapil_kk (4/18/2013)
oopes,

I think I have misunderstood the way to execute the given query...
After seeing comments First Attempts, second, third I executed one by one those insert query and forget about the GO batch separator... and I was thinking it was so easy question and count will give 3 but i was wrong and lost the points...
I think most of the think count value as 3 and executed in same fashion that I executed that's why load towards incorrect answer is more than correct answer...


Result of 3 will be given, if GO exists after this insert
-- Insert attempt #2
INSERT INTO dbo.Test (PrimKey, ValueCol)
VALUES (2, -2);
GO

Regards
IgorMi


Thanks Igor :)



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1444314
Posted Friday, April 19, 2013 7:03 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 7:43 AM
Points: 3,692, Visits: 4,824
Nice, simple question to end the week, thanks Hugo

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Post #1444368
Posted Friday, April 19, 2013 7:16 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 8:12 AM
Points: 520, Visits: 548
Another interesting question to finish the week, thanks!
Post #1444376
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse