Complicated Comments

  • This is really exciting question.

    I always had feelings that /* */ has priority on – . And construction like -- /* will lead SQL to ignore /*.

    I was wrong! Thank you for so useful information and good lesson.

    Artur

  • I'm glad that everyone seemed to like the question (and surprising answer); I wondered whether people would object to the obscure nature of the problem.

    This scenario came up when I started investigating the comparison of lines of text between stored procedures (http://jessesql.blogspot.com/2009/02/comparing-stored-procedures-part-1.html). I wanted to allow the possibility in the comparison of suppressing comments - that's when I discovered this rather odd feature of commenting.

  • TO Russell,

    What version of management studio did you use? In studio 2005 I got 1, 5, 8. I triple checked the executed code to make sure it was the same as the problem shown.

  • I'm using Microsoft SQL Server Management Studio version 9.00.2047.00

    I get 1, 4, 5 and 8. Where has your 4 gone?

  • Using Studio 9.00.3042.00

    PRINT '1' -- /* ;PRINT '2' */ ;PRINT '3' /* PRINT '4' --*/

    --/*

    PRINT '5'

    --*/

    /*

    PRINT '6'

    --/*

    */

    PRINT '7'

    --*/

    PRINT '8'

    yields a return of:

    1

    5

    8

    The parser only shows PRINT '1' , PRINT '5' and PRINT '8' in blue, the rest of the PRINT statements are Green (comments)

    SQL Server 2005 64-Bit on Sun 4600

  • I agree with D. Couturier; if someone brought me code for review with comments anything close to this, I'd send it back. It's nice to know the behavior, but keep it simple.

  • Irish Flyer (2/11/2009)


    Using Studio 9.00.3042.00

    PRINT '1' -- /* ;PRINT '2' */ ;PRINT '3' /* PRINT '4' --*/

    --/*

    PRINT '5'

    --*/

    /*

    PRINT '6'

    --/*

    */

    PRINT '7'

    --*/

    PRINT '8'

    yields a return of:

    1

    5

    8

    The parser only shows PRINT '1' , PRINT '5' and PRINT '8' in blue, hte rest of the PRINT statements are Green (comments)

    Hi, your code isn't quite the same as Jesse's original:

    PRINT '1' -- /* ;PRINT '2' */ ;PRINT '3' /*

    PRINT '4' --*/

    --/*

    PRINT '5'

    --*/

    /*

    PRINT '6'

    --/*

    */

    PRINT '7'

    --*/

    PRINT '8'

    Here Print '4' is on a separate line.

    I tried your example and my result was like yours. That line break is obviously making the difference. Good old Microsoft - keep us on our toes...

  • don't get this at all. I answered 1,4,5,7,8 and was amazed to get it wrong, so I have checked it using QA in 2000 SP4 and SSMS 2005 SP2, both show the print '7' as NOT greened out as a comment, but it does not print!

    Explain that one, the gui does not see it as a comment but it acts as one.

    CORRECTION - QA shoes it NOT greened out, SSMS shows it greened out, NEITHER print it!

    ---------------------------------------------------------------------

  • That's right!

    When I corrected the code to be

    PRINT '1' -- /* ;PRINT '2' */ ;PRINT '3' /*

    PRINT '4' --*/

    --/*

    PRINT '5'

    --*/

    /*

    PRINT '6'

    --/*

    */

    PRINT '7'

    --*/

    PRINT '8'

    the reply was

    1

    4

    5

    8

  • Given the inconsistancies noted, I guess it brings home the point that one should be consistant in coding. Either use the -- convention to comment a line, or use /* */ pairs for comments, but don't mix them. Consistancy is just good coding practice anyway.

  • Myles Sigal (2/11/2009)


    I agree with D. Couturier; if someone brought me code for review with comments anything close to this, I'd send it back. It's nice to know the behavior, but keep it simple.

    Thanks Myles Sigal. I'm glad I'm not alone.

    It's the --/* ... --*/ that got me.

    If our developpers here would write comments like that, I'd jump up to the ceiling.

    D. Couturier

    Database Administrator & Architect

  • Irish Flyer (2/11/2009)


    Given the inconsistancies noted, I guess it brings home the point that one should be consistant in coding. Either use the -- convention to comment a line, or use /* */ pairs for comments, but don't mix them. Consistancy is just good coding practice anyway.

    You definitely don't want to mix commenting styles, as this QoD shows. I'm coming around to the opinion that inline comments should be avoided completely. This is especially a problem when copying-and-pasting code into some text editors - inline comments can get wrapped to the next line, causing compilation problems, whereas traditional block quoting should always work ok (this is "in line" with what happened with your initial evaluation of the code - please forgive the pun).

  • We had an error just last week where some code was commented out, tested and successful in test environment, then copy and paste to production and it failed due to a line wrap, something like this

    FROM table1 t1 -- INNER JOIN table2 t2 ON t1.id = t2.id

    but wrapped like this

    FROM table1 t1 -- INNER JOIN table2 t2

    ON t1.id = t2.id

    - Myles

  • Interesting and fun. My answer was 1,4,5,7,8 --wrong. I am amazed to see so many smarties out there get it right!

    I checked the syntax highlighting in various tools I have:

    sqldbx 1,4,5,7,8

    oracle sql developer 1,4,5,7,8

    SSMS 9.00.1399.00 1,4,5,8

    Notepad++ 1,4,5,7,8

    I actually tried the equivalent in plsql. The results were 1,4,5,7,8

    Someone put in good some extra effort at MS to make it work like this.

    select 1 from dual; -- /* ;PRINT '2' */ ;PRINT '3' /*

    select 4 from dual; --*/

    --/*

    select 5 from dual;

    --*/

    /*

    select 6 from dual;

    --/*

    */

    select 7 from dual;

    --*/

    select 8 from dual;

  • FWIW, the Simple-Talk Code Prettifier on SQLServerCentral gets it wrong (highlights PRINT '7' as executable). SSMS (2K5 SP3) shows

    [font="Courier New"]PRINT '1' -- /* ;PRINT '2' */ ;PRINT '3' /*

    PRINT '4' --*/

    --/*

    PRINT '5'

    --*/

    /*

    PRINT '6'

    --/*

    */

    PRINT '7'

    --*/

    PRINT '8'

    [/font]

    I tried to work it out before trying SSMS and wrongly got 1,4,5,7 & 8 as many others did.

    Derek

Viewing 15 posts - 16 through 30 (of 53 total)

You must be logged in to reply to this topic. Login to reply