Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add a generated comment in select statement of mysql

I know it is possible to add comments to select statements as follows:

select /*my comment*/ id, name from myTable;

But is it possible to make this comment more flexible and dynamic?
for example, I want to add the bellow variable as a comment in a select statement in a procedure.

set @mytext = 'a dynamic comment generated by code';

The aim is to get more detail of the queries in the processlist when they consume a lot of time.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use a user variable only in place of a string literal.

To use it as a comment, you would have to format the SQL query as a string and concatenate the variable into the string, then execute that string using dynamic SQL.

SET @query = CONCAT('select /* ', @mytext, ' */ id, name from myTable');
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading