问题
Like EXECUTE IMMEDIATE
in Oracle, is there any way to execute code dynamically in a MySQL stored procedure?
I really want to use a prepared statement within a MySQL stored procedure, to generate a new SQL statement in each iteration of a loop.
回答1:
It actually doesn't work like what I wrote. I just code like:
set @preparedstmt = concat('SELECT tid, LENGTH(message) len FROM ? where tid=? and first=1');
prepare stmt from prepared_stmt;
execute stmt using v_tid;
drop prepare stmt;
Just take care of the table name,it shouldn't be replaced with the placeholder.So the @preparedstmt should be generated with concat method to make a statement,which is just replaced the parameters in conditions with placeholder,but not the table name.
来源:https://stackoverflow.com/questions/7567277/is-there-a-way-to-execute-code-dynamically-in-mysql-similar-to-execute-immedia