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


Triggers 1


Triggers 1

Author
Message
Christian Buettner-167247
Christian Buettner-167247
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3657 Visits: 3889
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
Dineshbabu
Dineshbabu
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: 1208 Visits: 569
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..
kapil_kk
kapil_kk
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: 3157 Visits: 2766
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 :-P

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Hugo Kornelis
Hugo Kornelis
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11338 Visits: 12006
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
Igor Micev
Igor Micev
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 6016 Visits: 5084
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... Sad
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

Igor Micev,
SQL Server developer at Seavus
My blog: www.igormicev.com
Tom Thomson
Tom Thomson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14740 Visits: 12238
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

Hugo Kornelis
Hugo Kornelis
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11338 Visits: 12006
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
kapil_kk
kapil_kk
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: 3157 Visits: 2766
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... Sad
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 Smile

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCrazy Eights
SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)

Group: General Forum Members
Points: 9177 Visits: 7283
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”
Mike Hays
Mike Hays
Right there with Babe
Right there with Babe (777 reputation)Right there with Babe (777 reputation)Right there with Babe (777 reputation)Right there with Babe (777 reputation)Right there with Babe (777 reputation)Right there with Babe (777 reputation)Right there with Babe (777 reputation)Right there with Babe (777 reputation)

Group: General Forum Members
Points: 777 Visits: 764
Another interesting question to finish the week, thanks!
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