Dynamic Query

  • TomThomson

    SSC Guru

    Points: 104773

    Hugo Kornelis (9/24/2013)

    L' Eomot Inversé (9/24/2013)

    Interesting question.

    I knew the answer, but Jens-Peter's comment made me wonder: he's quite right, the behavious is not documented on the page referenced. Nor is it documented on the -- comment BOL page. I wonder if it is documented anywhere?

    I think it is clearly documented.

    The -- page specifies that the rest of the text until the end of the line is ignored.

    The /* */ page specifies that the text between opening and closing mark is ignored, with the explicit exception that nested comments are recognised.

    Add one and one together, and it is clear that /* ... */ is parsed for other /* and */ sequences, while -- ... (end of line) is not parsed at all.

    On the -- BOL page we have the plain and simple statements (two sentences) "Use two hyphens (--) for single-line or nested comments. Comments inserted with -- are terminated by the newline character."

    That seems to me to suggest that -- comments can be nested (although I know the can't). Yes, it says the comment text isn't parsed. However, the /* page also says that the comment text isn't parsed (" The text between the /* and */ is not evaluated by the server.") although it does go on to blatantly contradict itself further down the page. You may think that that is clear and simple documentation that documents the fact that -- trumps \*. I don't.


  • PHYData DBA


    Points: 7541

    Interesting and easy question.

    The answer is only true because "--" creates comments only for the same line.

    The explanation is lacking in not pointing out the differences between using "--" and "/* */" that make the answer correct.

  • Revenant


    Points: 42467

    Nice and easy - thanks, Samith!

  • zerko


    Points: 1524

    Samith, discover more fun of combining block comments and in-line comments ... my basic rule, to avoid the error your example raises, always put an inline comment just in front of the block close, like so.



    Try changing the value of @debug from '--' to '' in the following code:

    declare @debug varchar(2) = '--'

    declare @var varchar(1000)

    set @var = @debug + '/*

    select ''Samith'' name --*/'

    set @var = @var + ' select ''sas'''

    exec (@var)



  • Tee Time

    Hall of Fame

    Points: 3693

    Good one, thanks!


    SSC Guru

    Points: 281243

    EZ PZ

    Jason...AKA CirqueDeSQLeil
    I have given a name to my pain...MCM SQL Server, MVP
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • PChiragS

    SSCarpal Tunnel

    Points: 4965

    Toreador (9/24/2013)

    I got this wrong because I assumed the "*/" was only on a new line because it had been wrapped by the narrow display 🙁


  • jfgoude


    Points: 2586

    pchirags (9/28/2013)

    Toreador (9/24/2013)

    I got this wrong because I assumed the "*/" was only on a new line because it had been wrapped by the narrow display 🙁


    same for me

Viewing 8 posts - 31 through 38 (of 38 total)

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