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 Thursday, April 18, 2013 10:26 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:32 PM
Points: 5,794, Visits: 8,006
Comments posted to this topic are about the item Triggers 1


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1444226
Posted Thursday, April 18, 2013 10:29 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 11:47 PM
Points: 20,458, Visits: 14,082
Thanks Hugo. Good question.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1444227
Posted Thursday, April 18, 2013 10:54 PM


SSC Eights!

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

Group: General Forum Members
Last Login: 2 days ago @ 5:16 AM
Points: 905, Visits: 553
Thanks Hugo..

If insert statement was within Explicit transaction then even first row will not be inserted and the result of final query will be 0. Am I right?


Whether XACT_ABORT can play any role here?



--
Dineshbabu
Desire to learn new things..
Post #1444229
Posted Thursday, April 18, 2013 11:40 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, April 14, 2014 11:50 PM
Points: 1,867, Visits: 2,275
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...



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


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, February 06, 2014 4:15 AM
Points: 1,242, Visits: 1,546
Very good question hugo

In the starting of question, i come with answer 3. But suddenly i check "go", and game changed.

Good question with good trick.




Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
www.GrowWithSql.com

Post #1444238
Posted Friday, April 19, 2013 12:29 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:32 PM
Points: 5,794, Visits: 8,006
Dineshbabu (4/18/2013)
If insert statement was within Explicit transaction then even first row will not be inserted and the result of final query will be 0. Am I right?

Yes (assuming you start the transaction after the table creation and before the first insert). The ROLLBACK TRAN in the trigger will rollback the active transaction. In the code as posted, each INSERT starts its own implicit transaction which is committed (or rolled back) at the end of the statement. If you explicitly start a transaction before the first insert, then no implicit transactions will be started. And the ROLLBACK TRAN in the trigger will roll back that explicit transaction.

Whether XACT_ABORT can play any role here?

No. XACT_ABORT has an effect on errors that do not normally cause a rollback in an explicit transaction. In the code as posted, there is no explicit transation, so that alone means that XACT_ABORT has no effect. Adding an explicit transaction doesn't change that, as the error in this code already causes a rollback - so there is nothing for XACT_ABORT to do that the trigger code doesn't already do.

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!)



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


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:32 PM
Points: 5,794, Visits: 8,006
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!



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


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 1:16 AM
Points: 2,826, Visits: 2,836
Thanks for the question Hugo - nice and easy to end the week on

-------------------------------
Posting Data Etiquette - Jeff Moden
Smart way to ask a question

There are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan
I would never join a club that would allow me as a member - Groucho Marx
Post #1444241
Posted Friday, April 19, 2013 1:22 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, February 06, 2014 4:15 AM
Points: 1,242, Visits: 1,546
Hugo, Question title is "Triggers 1", so everybody look at trigger (even be also). But trick was in "GO"

I think question title should be "GO".





Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
www.GrowWithSql.com

Post #1444249
Posted Friday, April 19, 2013 1:40 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:32 PM
Points: 5,794, Visits: 8,006
Danny Ocean (4/19/2013)
Hugo, Question title is "Triggers 1", so everybody look at trigger (even be also). But trick was in "GO"

I think question title should be "GO".


Try what happens if you use a CHECK constraint instead of a trigger to prevent negative values.


By the way, my questions never intentionally contain "tricks". I try to test knowledge. In this case, I focus on how a rollback from a trigger affects a batch.
I realize, from another posters' comment, that the comments in the code were misleading to some. I am really sorry for that. I absolutely do not want to trick people; I want to test their knowledge (and add to it, of course)



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1444256
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse