Preserve Comments in MySQL Stored Procedures

Comments in any code are good practice and can help guide yourself and others through more complicated processes.

Single and multi-line comments can be included in MySQL stored procedures using the following syntax:

# a single-line comment

select 1;

/* a
   multi-line
   comment
*/

When creating stored procedures for a MySQL database using a GUI, like MySQL Browser, any comments added are preserved (as you would expect). However, if you use the command line client you may have noticed that comments are stripped out. The command line client removes any comments before executing the script so comments are lost.

Thankfully there is a simple solution to preserve any comments. When connecting to your MySQL server simply add the option --comments and comments will now be preserved:

mysql -u username -p --comments

The default is --skip-comments which explains why comments are normally removed. There is more information about these options and others on the MySQL website.

To make sure you remember to add the --comments option you can create a batch file to run the command line tool with the option included.

This entry was posted in Programming and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted August 10, 2013 at 11:17 pm | Permalink

    Hi
    This is also useful when using MariaDB 10’s SQL_ERROR_LOG plugin. When a query fail, it is log. Comments can make queries more easy to search.

  2. John Doe
    Posted November 24, 2016 at 12:49 pm | Permalink

    Excellent ! Exactly what I was looking for. Works well

Post a Comment

*
*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

* = Required