Skip to content

Instantly share code, notes, and snippets.

@ramsey
Created October 28, 2014 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramsey/e016b272589d1cc28f8e to your computer and use it in GitHub Desktop.
Save ramsey/e016b272589d1cc28f8e to your computer and use it in GitHub Desktop.
Example of declaring a continue handler in MySQL and rolling back or committing, based on status of _rollback
DROP PROCEDURE IF EXISTS test_rollback_proc;
DELIMITER //
CREATE PROCEDURE test_rollback_proc()
BEGIN
DECLARE `_rollback` BOOL DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET `_rollback` = 1;
START TRANSACTION;
/* SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Custom error'; */
IF `_rollback` THEN
SELECT 'rolling back' AS status;
ROLLBACK;
ELSE
SELECT 'committing' AS status;
COMMIT;
END IF;
END//
DELIMITER ;
CALL test_rollback_proc();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment