Execute the result of a sql select in MySql
问题 I am trying to get MySql to execute the result of the below statement as further sql statements. I believe in oracle sqlplus this is achieved using the spool function. How is this achieved in Mysql ? select concat('OPTIMIZE TABLE `', ist.TABLE_SCHEMA,'`.', ist.TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES ist where table_schema = 'my_schema'; 回答1: You have to use prepared statements. SET @s:=''; SELECT @s:=concat(@s, 'OPTIMIZE TABLE `', ist.TABLE_SCHEMA,'`.', ist.TABLE_NAME, ';') FROM