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

后端 未结 2 975
失恋的感觉
失恋的感觉 2021-01-15 21:08

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 wi

2条回答
  •  耶瑟儿~
    2021-01-15 21:45

    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.

提交回复
热议问题