简体   繁体   中英

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM