在MySQL事务中执行

时间:2013-01-08 03:48:18

标签: mysql stored-procedures transactions

我正在与MySQL存储过程和PREPARE / EXECUTE语句对搏斗。我正在尝试运行下面的(简化)代码,以便创建一个存储过程,该过程将多个查询封装到具有回滚的事务中。我继续得到

 Error Code: 1064
 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 'goodID;

如果我删除了事务,处理程序一切都很好。如果我删除了EXECUTE语句,则可以创建该过程。

我在这里缺少什么?

DELIMITER $$

USE `casc`$$

DROP PROCEDURE IF EXISTS `sp_T_MergeMemberIDs`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_T_MergeMemberIDs`(IN goodID VARCHAR(8), OUT param_sp_success TINYINT)

BEGIN

   DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
   DECLARE EXIT HANDLER FOR SQLWARNING ROLLBACK;

   START TRANSACTION;

      SET param_sp_success = 0;

      SET @SQL=
      "SELECT * FROM member
      WHERE memberID = ?";

      PREPARE stmt FROM @SQL;

      EXECUTE stmt USING goodID;

      -- queries executed here using the same parameter
      -- omitted for simplicity

      SET param_sp_success = 1; 

   COMMIT;

END$$

1 个答案:

答案 0 :(得分:1)

据我记忆,您需要在execute ...using中使用会话用户变量:

...
PREPARE stmt FROM @SQL;
SET @tmp_var = goodID;
EXECUTE stmt USING @tmp_var;
...