1

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.

SQB
  • 3,926
  • 2
  • 28
  • 49
Eric_Chen
  • 227
  • 1
  • 4
  • 13

2 Answers2

1

MariaDB is supporting EXECUTE IMMEDIATE ince 10.2 KB doc is : https://mariadb.com/kb/en/execute-immediate/

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.

Eric_Chen
  • 227
  • 1
  • 4
  • 13