admin

MySQL ::在Variable中运行SQL语句

sql

我正在MySQL 5.1.40中创建一个存储过程,该过程需要运行许多SQL语句。

一个经常重复的特定语句会清除临时表:

DELETE FROM w_projection_temp WHERE user_id = uid;

UID 变量是SP的IN参数。

是否可以将整个SQL语句分配给另一个变量,然后以某种方式运行(评估?)该新变量?例如:

DECLARE clear_temp VARCHAR;
SET clear_temp = 'DELETE FROM w_projection_temp WHERE user_id = uid;'

MTIA


阅读 40

收藏
2021-06-07

共1个答案

admin

PREPARE clear_temp FROM "DELETE FROM w_projection_temp WHERE user_id = ?"
EXECUTE clear_temp USING uid;
DEALLOCATE PREPARE clear_temp;
2021-06-07