I am calling Stored procedure from Trigger and I get the following error:
Dynamic SQL is not allowed in stored function or trigger
Why is this happening, the dynamic SQL is being executed in Stored procedure, which is called from Trigger. Maybe this is the problem, if so is there any workaround?
Edit (added code):
Here is Trigger from the master table:
-- 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
$$
And here is the store procedure that is called from the trigger:
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