Complicated Comments

  • Comments posted to this topic are about the item Complicated Comments

  • 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'

  • Nice question.

    What a counterintuitive way this behaves!

  • 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

  • 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).

  • 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.

  • 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

  • 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.

  • 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:

  • 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

  • For anyone who cares to know, this behavesas stated in the explaination both for 2005 and 2008 of ma machine (latest services packs).

  • 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

  • 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

  • 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.

    Tom Garth
    Vertical Solutions[/url]

    "There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers
  • 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