JustAPup
JustAPup

Reputation: 1780

How to insert a comment in the middle of a SQL statement?

I have a long and complicated SQL statement, in the middle of it, I want to insert a comment.

For example:

SELECT * FROM TABLE_A
WHERE Column1 IN ('Code1' --Comment here--, 'Code2' --Comment here--) 

Please notice how the part after 'Code1' --Comment here-- is greyed out. That's because the whole line after -- is considered a comment.

Currently I have to do something like this:

SELECT * FROM TABLE_A
WHERE Column1 ='Code1' --Comment here--
   OR Column1 ='Code2' --Comment here--

I wonder if anyone has a better way to write this. Thanks..

Upvotes: 2

Views: 3076

Answers (3)

Gordon Linoff
Gordon Linoff

Reputation: 1269793

For your case:

SELECT *
FROM TABLE_A
WHERE Column1 IN ('Code1' /* CODE1 */, 'Code2' /* CODE2 */) 

I will add that this type of embedded comment is likely to make the code harder to follow rather than easier. One such comment is probably okay, but if you want to include comments on all values, I would go with the multi-line version in your question.

Normally, by the way, the /* . . . */ is used for multi-line comments or to quickly comment out a block of code.

Upvotes: 2

Chris Combs
Chris Combs

Reputation: 529

Assuming you're using MySQL,

mysql> SELECT 1 /* this is an in-line comment */ + 1;

Source: https://dev.mysql.com/doc/refman/5.7/en/comments.html

Upvotes: 4

Dave.Gugg
Dave.Gugg

Reputation: 6771

Use inline comments for this:

SELECT * FROM TABLE_A
WHERE Column1 IN ('Code1' /*Comment here*/, 'Code2' /*Comment here*/) 

Upvotes: 3

Related Questions