12

存储过程

DELIMITER $$

CREATE PROCEDURE `lms`.`leads_to_bak` ()
BEGIN
SET @table1 = (SELECT `tabler_name` FROM `sets` WHERE `on_off`=0 LIMIT 1);
SET @table2 = CONCAT(@table1, '_bak');
SET @SQL1 = CONCAT('INSERT INTO ',@table2, '(', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'lead_id,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @table2), ')', ' SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'lead_id,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @table1), ' FROM ', @table1);
PREPARE stmt FROM @sql1;
EXECUTE stmt;
END$$

DELIMITER ;

触发

DELIMITER $$
USE `lms`$$

CREATE TRIGGER `lms`.`after_insert_into_leads`
AFTER INSERT ON `sets` FOR EACH ROW
BEGIN
CALL lms.leads_to_bak();
END$$

DELIMITER ;

问题

我在制作暗示将执行触发器和存储过程时收到Error Code: 1336. Dynamic SQL is not allowed in stored function or trigger错误消息。INSERT我假设问题是这里的动态 SQL:

PREPARE stmt FROM @sql1;
EXECUTE stmt;

我环顾四周,stackoverflow上有一个关于问题的线程,但没有答案。有人对可行的解决方法有任何建议吗?

4

2 回答 2

8

对于 MySQL 函数中缺少动态 SQL 没有很好的解决方法,只是笨拙的 cludges。有些事情仍然完全无法解决,例如在 SQL 查询中使用动态计算的字段名或表名。是的,偶尔需要做这种事情!

并且不要试图通过将动态 SQL 放入存储过程并包装在函数或触发器中来作弊,就像问题提出者所尝试的那样 - MySQL 太聪明了,会给你通常晦涩的错误消息。相信我,我去过所有的房子。

来自 Oracle PL/SQL 和 MS SQL Server 背景,我非常怀念 PL/SQL 和(在某种程度上)T-SQL 为编写过程 SQL 提供的丰富性。

于 2015-05-05T18:16:57.393 回答
4

在过程定义中,您需要存储所有IN/OUT变量。

改变:

CREATE PROCEDURE `lms`.`leads_to_bak` ()

至:

CREATE PROCEDURE `lms`.`leads_to_bak` (
    IN table1 varchar(32),
    IN table2 varchar(32),
)

然后调用这样做:

CALL `lms`.`leads_to_bak`('table1', 'table2')

用你自己的替换字符串。

使用存储过程的目的是防止使用严格类型的数据进行 SQL 注入。如果您只在参数列表中发送严格类型的输入变量,则技术上不需要在存储过程中准备它。

这样,您可以在存储过程调用之前处理字符串操作。让你的存储过程保持瘦身!

这是我的一个存储过程的示例:

DELIMITER ;
DROP PROCEDURE IF EXISTS `save_player`;
DELIMITER //

CREATE PROCEDURE `save_player` (
IN uid int(15) UNSIGNED,
IN email varchar(100),
IN name varchar(100),
IN passwd char(96),
IN state ENUM('active','suspended','deleted'),
IN user_role ENUM('gamemaster','moderator','player'),
IN locale ENUM('en','fr'),
IN lvl tinyint(1),
IN hp bigint(20),
IN reborn tinyint(1),
IN cross_ref varchar(12),
IN email_verified tinyint(1),
OUT new_id  int(15) UNSIGNED
)
BEGIN
   DECLARE date_deleted timestamp DEFAULT NULL;
   IF uid > 0 AND EXISTS (SELECT id FROM user WHERE `id`= uid) THEN
      IF state = 'deleted' THEN
        SET date_deleted = CURRENT_TIMESTAMP;
      END IF ;
      UPDATE `user` SET
        `email` = email,
        `name` = name,
        `passwd` = passwd,
        `state` = state,
        `user_role` = user_role,
        `locale` = locale,
        `lvl` = lvl,
        `hp` = hp,
        `reborn` = reborn,
        `cross_ref` = cross_ref,
        `email_verified` = email_verified,
        `date_deleted` = date_deleted
      WHERE `id` = uid;
      SET new_id = uid;
   ELSE
      INSERT INTO user (`email`, `name`, `passwd`, `state`, `user_role`, `locale`, `lvl`, `hp`, `reborn`, `cross_ref`, `email_verified`, `date_created`)
             VALUES (email, name, passwd, state, user_role, locale, lvl, hp, reborn, cross_ref, email_verified, NOW());
      SELECT LAST_INSERT_ID()  INTO new_id;
   END IF;
 END //
DELIMITER ;
于 2012-09-24T15:56:29.673 回答