我从触发器调用存储过程,我收到以下错误:
存储函数或触发器中不允许使用动态 SQL
为什么会发生这种情况,动态SQL是在存储过程中执行的,该存储过程是从触发器调用的。也许这就是问题所在,如果是这样,有什么解决方法吗?
编辑(添加代码):
这是来自主表的触发器:
-- Trigger DDL Statements
DELIMITER $$
USE `TestaDataBase`$$
CREATE TRIGGER `TestaDataBase`.`UpdateAuxilaryTable`
AFTER INSERT ON `MainTable` FOR EACH ROW
BEGIN
/* Here we call stored procedure with parameter id of newly inserted row. */
CALL TestProcedure('Year', 'Person', 'IdPerson', NEW.IdData);
END
$$
这是从触发器调用的存储过程:
DELIMITER $$
CREATE PROCEDURE `TestDataBase`.`TestProcedure` (IN attribute CHAR(64), IN tableName CHAR(64), IN IdTable CHAR(64), IN IdLastRow MEDIUMINT)
BEGIN
DECLARE selectedValue MEDIUMINT;
SET @statement = CONCAT('SELECT ', attribute, ' FROM ', tableName, ' WHERE ', IdTable, ' = ', IdLastRow, ' INTO selectedValue');
PREPARE statementExecute FROM @statement;
EXECUTE statementExecute ;
...
...
END