问题描述
像Oracle中的EXECUTE IMMEDIATE
,有什么方法可以在MySQL存储过程中动态执行代码吗?
Like EXECUTE IMMEDIATE
in Oracle, is there any way to execute code dynamically in a MySQL stored procedure?
我真的很想在MySQL存储过程中使用准备好的语句,以在每次循环迭代中生成新的SQL语句.
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.
推荐答案
它实际上不像我写的那样工作.我只是这样的代码:
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;
只需要注意表名,就不能用占位符代替.因此,@ preparedstmt应该使用concat方法生成,以进行声明,只是将条件中的参数替换为占位符,而不是表名.
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.
这篇关于有没有一种方法可以在MySQL中动态执行代码,类似于“立即执行"操作.在甲骨文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!