在 mysql 上创建触发器时,存储函数或触发器问题中​​不允许使用动态 SQL

Dynamic SQL is not allowed in stored function or trigger issue while create trigger on mysql

我在 mysql 上创建触发器时出现异常,在下面粘贴我的触发器片段。我希望这个触发器中的准备语句抛出这个异常,请纠正我语法中的任何错误。

 DELIMITER $$

 CREATE DEFINER=`root`@`localhost` TRIGGER mysql_common.mysql_alert_trigger after insert on mysql_common.alert FOR EACH ROW

 BEGIN

 set @col_string = "col1,col2,col3";
 set @val_string = "('val1','val2','val3')";

 SET @s := CONCAT('INSERT into   msql_common.seperated_processlist(',@col_string,') values ', @val_string);
 PREPARE dynamic_statement FROM @s;
 EXECUTE dynamic_statement;
 DEALLOCATE PREPARE dynamic_statement;

 END $$
 DELIMITER ;
DELIMITER $$

 CREATE DEFINER=`root`@`localhost` TRIGGER mysql_common.mysql_alert_trigger after insert on mysql_common.alert FOR EACH ROWEACH ROW

 BEGIN

 call SOME_PROC();
 END $$
 DELIMITER ;

您可以使用您的业务逻辑编写一个过程并从触发器调用该过程,如上所示code.with您的特定输入和输出参数。

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `SOME_PROC`()
BEGIN
    set @col_string = "col1,col2,col3";
 set @val_string = "('val1','val2','val3')";

 SET @s := CONCAT('INSERT into   msql_common.seperated_processlist(',@col_string,') values ', @val_string);
 PREPARE dynamic_statement FROM @s;
 EXECUTE dynamic_statement;
 DEALLOCATE PREPARE dynamic_statement;

END$$
DELIMITER ;

希望这会有所帮助。