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.
CodePudding user response:
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;