I'm sort of new to SQL and stored procedures. I'm trying to set some variables to use later on in a transaction while also setting a rollback variable on an exception, but I'm not able to do this.
I don't know where the error is because when I switch the how_many
section after the _rollback
, The error changes.
What's wrong with the way I'm declaring variables here?
DELIMITER $$
DROP PROCEDURE IF EXISTS `do_thing` $$
CREATE PROCEDURE `do_thing`()
BEGIN
DECLARE how_many INT;
SELECT COUNT(*) FROM things INTO how_many;
-- Prepare roleback.
DECLARE `_rollback` BOOL DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET `_rollback` = 1;
-- Start transaction.
START TRANSACTION;
-- Do all the things.
IF `_rollback` THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
END $$
DELIMITER ;
Best Answer
Try this :