ellswrth
ellswrth

Reputation: 13

EXECUTE in MySQL Transaction

I'm wrestling with MySQL stored procedures, and the PREPARE / EXECUTE statement pair. I'm attempting to run the (simplified) code below in order to create a stored procedure that will encapsulate several queries into a transaction with rollback. I continue to get

 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;

If I remove the transaction and handlers all is well with the code. If I remove the EXECUTEstatement the procedure can be created.

What am I missing here?

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$$

Upvotes: 1

Views: 1056

Answers (1)

a1ex07
a1ex07

Reputation: 37364

As far as I remember, you need to use session user variable in execute ...using :

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

Upvotes: 1

Related Questions