4

I'm using -- and # for comments in my SQL scripts but when I run, it returns error messages about syntax errors. When I take the comments out it runs fine. For examples below cause errors.

#################################################
# DELETE RECORDS FROM TABLE
#################################################

-- DELETE RECORDS FROM TABLE WHERE EXTRACT RECORDS CONTAIN DELETE

The only comment I can do is # at the beginning of a line which I guess is sufficient but I want to know why this doesn't work. Did this all the time in SQLServer.

5
  • I don't know about any specific reason for # comments causing errors (I've had that same issue). But I recommend you change all your comments to -- to avoid any problems. Commented Jan 13, 2014 at 20:36
  • This syntax is fine in mysql. What errors are you getting? Commented Jan 13, 2014 at 20:39
  • Yes, strange - # should work: dev.mysql.com/doc/refman/5.1/en/comments.html Commented Jan 13, 2014 at 20:39
  • Could you post more of the script, that is very odd indeed Commented Jan 13, 2014 at 20:40
  • Possible duplicate of stackoverflow.com/questions/6001390/… Commented Jan 13, 2014 at 20:47

4 Answers 4

6

Both ways are acceptable for commenting out lines in MySQL scripts. See MySQL's documentation on comments. The hash at the start of the line is acceptable, as is the double-hyphen as long as it is followed by a space, e.g.:

-- This line is valid a comment   

Not putting a space after the double hyphen will cause errors in mysql.

--This line is NOT a valid comment

If you are putting a space after the double-hyphens, there must be some other reason for the syntax errors. The example you posted works for me in MySQL without errors.

Sign up to request clarification or add additional context in comments.

1 Comment

The example works for me. The space-issue is something I encounter often (since it comments the line but is still read as an error). Please check this answer as correct @Bob
1

Comments in SQL start -- i.e. What you have and that works

But not starting with '#'

So put -- to start a comment

This is when the database is running in ANSI mode (I guess)

Comments

0

Try to do this otherwhise..

-- #################################################
-- # DELETE RECORDS FROM TABLE
-- #################################################

-- DELETE RECORDS FROM TABLE WHERE EXTRACT RECORDS CONTAIN DELETE

Comments

0

According to the documentation # is allowed for comments but you may need to add a space between the first and the second one eg: # ######

1 Comment

The OP is using MySQL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.