Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comments are causing errors in MySQL Script

Tags:

mysql

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.

like image 981
Bob Doherty Avatar asked Sep 11 '25 06:09

Bob Doherty


2 Answers

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.

like image 131
lreeder Avatar answered Sep 13 '25 04:09

lreeder


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)

like image 42
Ed Heal Avatar answered Sep 13 '25 04:09

Ed Heal