无法在MySQL存储过程中执行更改表

时间:2019-07-10 02:19:38

标签: mysql sql stored-procedures

我正在尝试在MySQL存储过程中使用alter table命令,
我使用CONCAT构成了我需要在过程中执行的查询,
如果我在存储过程之外运行它,则形成的alter查询工作正常。
如何在过程中执行它?

以下是我的摘录-

CREATE DEFINER=`root`@`localhost` PROCEDURE `alter_table_in_sp`()
BEGIN
    DECLARE snapshot_year INT;
    DECLARE partition_name VARCHAR(17);
    DECLARE addPartition VARCHAR(100);

    SET snapshot_year := DATE_FORMAT(NOW(),'%Y');
    SET partition_name := CONCAT('snapshot_',snapshot_year);

    SET addPartition := CONCAT('ALTER TABLE snapshot ADD PARTITION (PARTITION ', partition_name, ' VALUES IN (', snapshot_year,'))');
    #EXECUTE addPartition;

    #PREPARE _stmt FROM @addPartition;
    #EXECUTE _stmt;
    #DEALLOCATE PREPARE _stmt;
END

我尝试了以下-

1。

EXECUTE addPartition 

错误-

Error Code: 1243. Unknown prepared statement handler (addPartition) given to EXECUTE

2。

PREPARE _stmt FROM @addPartition;
EXECUTE _stmt;
DEALLOCATE PREPARE _stmt;

错误-

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1

参考-
Stored Procedure with ALTER TABLE

0 个答案:

没有答案