Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Complicated Comments


Complicated Comments

Author
Message
Charles Kincaid
Charles Kincaid
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1041 Visits: 2383
Good point. But then

/*
Good comment not.
-/**/*/

does work. Does this remind you of the Perl parser?

ATBCharles Kincaid
george sibbald
george sibbald
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6328 Visits: 13687
Charles Kincaid (2/11/2009)
Let me see if I can show what is going on here and why. Lets number the lines

(00) PRINT '1' -- /* ;PRINT '2' */ ;PRINT '3' /*
(01) PRINT '4' --*/
(02) --/*
(03) PRINT '5'
(04) --*/
(05) /*
(06) PRINT '6'
(07) --/*
(08) */
(09) PRINT '7'
(10) --*/
(11) PRINT '8'


Line (00) has an inline comment starting with the double dash so 2 an 3 are in the comment
Same for (01) so 4 does print just like 1.
Line (02) has an inline comment so the block comment starter is par of the comment and is ignored. Same for (04) and (10).
Line (03) is not in any comment and gets executed. Same goes for line (11).
Line (05) starts a block comment. Lines (06) and (07) are part of the block comment.
Line (08) stops the block comment.


but line 09 is NOT executed so it is still part of the commented out code....................

---------------------------------------------------------------------
Tom Garth
Tom Garth
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1013 Visits: 1499
Charles Kincaid (2/11/2009)
Good point. But then

/*
Good comment not.
-/**/*/

does work. Does this remind you of the Perl parser?



That's not as bad as it seems when you break it out.


/*
Good comment not.
-
/*
*/
*/



I've never had the pleasure of working with the Perl parser. Does it defy logic?

Tom Garth
Vertical Solutions

"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

Charles Kincaid
Charles Kincaid
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1041 Visits: 2383
I wrote some stuff in Perl as that was what was available on the web server. It was not that bad to write code. You start with the inmost thing. Then surround with the next layer of processing. Then surround that with the next. Etc. Very powerful. I had very intense routines in a single line of code. Nice.

Now come back a couple of months later and need to make one little change. w00t Trying to uncoil those lines to remember what they did. It's not so easy to read what you wrote. Crazy

Don't get me wrong here. Perl is fine. Many people use it and find it easy. I did not. It's the fault of me and not Perl.

ATBCharles Kincaid
archie flockhart
archie flockhart
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1253 Visits: 1150
Here's an interesting combination:

This code works perfectly OK ( the unmatched block comment marker causes no problems because it is part of an inline comment):


print '1'
--/*note unmatched block comment marker on this line, following an inline comment marker
print '2'
print '3'


result: 1 2 3

Now, suppose we are asked to disable the part of the code that prints 1 and 2 ... easy, we say Smile ... just put block comment markers around it ...


/*
print '1'
--/*note unmatched block comment marker on this line, following an inline comment marker
print '2'
*/
print '3'



result : Server: Msg 113, Level 15, State 1, Line 1
Missing end comment mark '*/'.

oops !


My attempt to explain the behaviour is: as soon as you are withiin a BLOCK COMMENT, the system ignores all INLINE COMMENT markers. For most statements, this doesn't matter, because the statement will not be executed anyway since it is part of the BLOCK COMMENT.

But START BLOCK COMMENT /* or END BLOCK COMMENT */ instructions which look as though they should be ignored because they follow a "--" , are actually processed, and may open a nested comment block or close the existing block or cause a syntax error.

Does that sound about right ?
Marcin Zawadzki
Marcin Zawadzki
SSC-Enthusiastic
SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)

Group: General Forum Members
Points: 196 Visits: 79
Nice feature to get trapped on migration from SQL 2000 to 2005.
Tom Garth
Tom Garth
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1013 Visits: 1499
archie flockhart (2/12/2009)
Here's an interesting combination:

This code works perfectly OK ( the unmatched block comment marker causes no problems because it is part of an inline comment):


print '1'
--/*note unmatched block comment marker on this line, following an inline comment marker
print '2'
print '3'


result: 1 2 3

Now, suppose we are asked to disable the part of the code that prints 1 and 2 ... easy, we say Smile ... just put block comment markers around it ...


/*
print '1'
--/*note unmatched block comment marker on this line, following an inline comment marker
print '2'
*/
print '3'



result : Server: Msg 113, Level 15, State 1, Line 1
Missing end comment mark '*/'.

oops !


My attempt to explain the behaviour is: as soon as you are withiin a BLOCK COMMENT, the system ignores all INLINE COMMENT markers. For most statements, this doesn't matter, because the statement will not be executed anyway since it is part of the BLOCK COMMENT.

But START BLOCK COMMENT /* or END BLOCK COMMENT */ instructions which look as though they should be ignored because they follow a "--" , are actually processed, and may open a nested comment block or close the existing block or cause a syntax error.

Does that sound about right ?



Yeah. But you gotta try this. You'll love it!


/*
print '1'
--/**/note unmatched block comment marker on this line, following an inline comment marker
print '2'
*/
print '3'



Tom Garth
Vertical Solutions

"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

archie flockhart
archie flockhart
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1253 Visits: 1150
Actually that just about makes sense, based on the assumption that "--" is ignored within block comments

so in this code:

/*
print '1'
--/**/note unmatched block comment marker on this line, following an inline comment marker
print '2'
*/
print '3'



... line 1 opens a comment block

print '1' is within the comment block so it doesn't print

on the line starting with "--" , the system ignores "--" , it then finds a complete (but empty) nested block comment ; followed by some text. The text is outside the nested comment, but INSIDE the original comment block that was started on line 1, so it does not cause an error.

print '2' is still within the original comment block

the next line ends the original comment block

and print '3' then executes
Jesse McLain
Jesse McLain
Old Hand
Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)

Group: General Forum Members
Points: 307 Visits: 567
This problem originally came to mind when I started work on a tool for comparing the text from stored procedures (http://jessesql.blogspot.com/2009/02/comparing-stored-procedures-part-1.html). I wanted to allow the option of omitting all comments from the comparison, which of course would require me to identify and parse out comments from the text. In my research, I came across the question of how to treat nested comments - the "order of operation" of inline versus block comments, nested blocks, etc., and that's when I came across the scenario that formed this question of the day. I have to say that I was surprised, almost to the point of shock, that commenting functioned this way.

Anyways, glad everyone seemed to like the question (maybe not getting it wrong, but in terms of learing something surprising).

Jesse McLain
jesse@jessemclain.com
www.jessemclain.com
www.jessesql.blogspot.com
DCouturier
DCouturier
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1190 Visits: 364
Jesse McLain (2/12/2009)
...
Anyways, glad everyone seemed to like the question (maybe not getting it wrong, but in terms of learing something surprising).


Thanks Jesse McLain for this question.

I surely learned something BigGrin. But I want my developers away from writing comments like this. Comments are there to help you, not to confuse you Tongue.

KISS is the key -> Keep It Simple Stupid. Doing so, any dba or developer can easily understand your code.

D. Couturier
Database Administrator & Architect
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search