Transaction in MySQL stored procedure
To execute a ROLLBACK in a MySQL stored procedure, we need to declare an exit handler
in the stored procedure. There are two types of handlers in the MySQL stored procedure.
sqlexception will execute when an error occurs during query execution, and sqlwarning will execute when the warning procedure is stored in MySQL. Let's see how we can have this block in a stored procedure.
DELIMITER $$ CREATE PROCEDURE `transaction_sp` () BEGIN DECLARE exit handler for sqlexception BEGIN -- ERROR ROLLBACK; END; DECLARE exit handler for sqlwarning BEGIN -- WARNING ROLLBACK; END; START TRANSACTION; -- ADD option 5 INSERT INTO product_option(product_id,option_id,required) VALUES(insertedProductID,5,0); SET poid = (SELECT LAST_INSERT_ID()); INSERT INTO product_option_value(product_option_id,product_id,option_id,option_value_id,quantity,subtract,price,price_prefix,points,points_prefix,weight,weight_prefix) VALUES(poid,insertedProductID,5,50,0,0,4.99,'+',0,'+',0,'+'); -- ADD option 12 INSERT INTO product_option(product_id,option_id,required) VALUES(insertedProductID,12,1); -- ADD option 13 INSERT INTO product_option(product_id,option_id,required) VALUES(insertedProductID,13,0); COMMIT; END $$
Nisar Nov 18 '13 at 10:57 2013-11-18 10:57
source share