
I have this sql file:
USE mydb;
DROP PROCEDURE IF EXISTS execSql;
DELIMITER //
CREATE PROCEDURE execSql (
IN sqlq VARCHAR(5000)
) COMMENT 'Executes the statement'
BEGIN
PREPARE stmt FROM sqlq;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
DELIMITER ;
When I try to run it with
# cat file.sql | mysql -p
I get
ERROR 1064 (42000) at line 6: 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 'sqlq;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END' at line 5
What am I doing wrong?
解决方案
you can only prepare and execute sql that's a string literal or a user variable that contains the text of the statement. try
USE mydb;
DROP PROCEDURE IF EXISTS execSql;
DELIMITER //
CREATE PROCEDURE execSql (
IN sqlq VARCHAR(5000)
) COMMENT 'Executes the statement'
BEGIN
SET @sqlv=sqlq;
PREPARE stmt FROM @sqlv;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
DELIMITER ;
本文解决了一个关于MySQL存储过程中预处理语句的语法错误问题。通过调整输入参数的使用方式,成功创建了一个能够执行动态SQL语句的存储过程。
1839

被折叠的 条评论
为什么被折叠?



