Is there a way to execute code dynamically in MySQL, similar to “execute immediate” in Oracle?

孤者浪人 提交于 2019-12-19 10:57:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!