我正在尝试在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