February 10, 2009 at 9:25 pm
Comments posted to this topic are about the item Complicated Comments
February 11, 2009 at 2:21 am
I could hardly believe I got it wrong, so I tried it in DbVisualiser ... I got 1,4,5,7,8 (just as I thought I would). Could this have something to do with using DbVisualiser (I guess not, since SQL is executed on the server anyway)?
My interpretation is as follows:
This opens a block comment... /*
...so this is not printed PRINT '6'
This is ignored since it is part of the block comment --/*
and it is not the closing of the block comment
This closes the block comment... */
...thanks to which this is printed PRINT '7'
This is an inline comment so it does nothing --*/
This is printed PRINT '8'
February 11, 2009 at 2:24 am
Nice question.
What a counterintuitive way this behaves!
February 11, 2009 at 2:44 am
Philippe Lauwers (2/11/2009)
I could hardly believe I got it wrong, so I tried it in DbVisualiser ... I got 1,4,5,7,8 (just as I thought I would). Could this have something to do with using DbVisualiser (I guess not, since SQL is executed on the server anyway)?My interpretation is as follows:
This opens a block comment... /*
...so this is not printed PRINT '6'
This is ignored since it is part of the block comment --/*
and it is not the closing of the block comment
This closes the block comment... */
...thanks to which this is printed PRINT '7'
This is an inline comment so it does nothing --*/
This is printed PRINT '8'
Philippe, can you try this in Query Analyzer/SSMS (whichever version you have) and see if you get a different answer (which you will) - then I guess it is something to do with DbVisualiser !??!?
Kev
February 11, 2009 at 2:44 am
Got it wrong , read the answer/explanation, didn't really believe it, so tried it in Query Analyser which gave the answer stated as the solution. But I still don't understand why ...
My step-by-step interpretation was the same as Phillipe's. And Query Analyser's own code-formatting algorithm takes the same view:the PRINT statements for 1,4,5,7 and 8 appear dark blue ( non-commented) while the statements for 2,3 and 6 appear light blue (commented).
February 11, 2009 at 2:46 am
This is a new bit of information to me.
I would have agreed with Phillippe's logic and so would have expected the 7 to show, but when I ran it in Management Studio you are correct. I didn't realise you could nest comments like this.
Thanks for a great question and increasing my obviously limited knowledge.
February 11, 2009 at 2:51 am
archie flockhart (2/11/2009)
Got it wrong , read the answer/explanation, didn't really believe it, so tried it in Query Analyser which gave the answer stated as the solution. But I still don't understand why ...My step-by-step interpretation was the same as Phillipe's. And Query Analyser's own code-formatting algorithm takes the same view:the PRINT statements for 1,4,5,7 and 8 appear dark blue ( non-commented) while the statements for 2,3 and 6 appear light blue (commented).
Archie - can you try in SSMS? I get a different code-formatting, which shows it coloured as it works.
BTW - I'm on SSMS 9.00.3042.00 i.e. 2005 SP2
Kev
February 11, 2009 at 2:56 am
In SQL 2000 Query Analyzer it formats the text as if PRINT '7' will happen, and yet it doesn't when you execute it. They must have updated things a bit in SSMS.
February 11, 2009 at 3:02 am
kevriley (2/11/2009)
Philippe, can you try this in Query Analyzer/SSMS (whichever version you have) and see if you get a different answer (which you will) - then I guess it is something to do with DbVisualiser !??!?
Kev
Sorry, I would love to but company policy ... DbVisualiser is the only tool I've got :angry:
This question gives me a better understanding of my past though :crazy:
February 11, 2009 at 4:36 am
Rachel Byford (2/11/2009)
In SQL 2000 Query Analyzer it formats the text as if PRINT '7' will happen, and yet it doesn't when you execute it. They must have updated things a bit in SSMS.
Thanks Rachel - I didn't have a version of 2000 I could lay my hands on to quickly check.....
Kev
February 11, 2009 at 6:27 am
For anyone who cares to know, this behavesas stated in the explaination both for 2005 and 2008 of ma machine (latest services packs).
February 11, 2009 at 6:43 am
Brilliant question! The nested comment scenario is probably more common that one might think. This is a non-intuitive behavior, yet great to be aware of. Thank You!
Regards, Jim C
February 11, 2009 at 6:59 am
Fiendish question, yet practical. (I got it wrong by choosing 1,4,5,7,8)
Nice work.
Thanks,
webrunner
-------------------
A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
February 11, 2009 at 7:27 am
That was FUN!
With all of the commenting that I use, permanent and temporary, I knew this would be a cinch. And like everyone else, I got it wrong. I think the logic is wrong, and that it shouldn't work that way, but what do I know.
I would like to hear from the 48% that got it right. Did any of you get it right without trying it first?
Thanks for a neat QOD.
February 11, 2009 at 7:38 am
This was a pretty good catch!
It got me :crazy:
Who the heck would use comments like that. I practice the KISS method which stands for "Keep It Simple Stupid". I use this as much as possible.
D. Couturier
Database Administrator & Architect
Viewing 15 posts - 1 through 15 (of 53 total)
You must be logged in to reply to this topic. Login to reply