mysql存储过程中使用事务
1 DROP PROCEDURE IF EXISTS test_sp1 2 CREATE PROCEDURE test_sp1( ) 3 BEGIN 4 DECLARE t_error INTEGER DEFAULT 0; 5 DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1; 6 7 START TRANSACTION; 8 INSERT INTO test VALUES(NULL, 'test sql 001'); 9 INSERT INTO test VALUES('1', 'test sql 002'); 10 11 IF t_error = 1 THEN 12 ROLLBACK; 13 ELSE 14 COMMIT; 15 END IF; 16 17 END
返回执行状态,即是提交了还是回滚了:
1 DROP PROCEDURE IF EXISTS test_sp1 2 CREATE PROCEDURE test_sp1( ) 3 BEGIN 4 DECLARE t_error INTEGER DEFAULT 0; 5 DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1; 6 7 START TRANSACTION; 8 INSERT INTO test VALUES(NULL, 'test sql 001'); 9 INSERT INTO test VALUES('1', 'test sql 002'); 10 11 IF t_error = 1 THEN 12 ROLLBACK; 13 ELSE 14 COMMIT; 15 END IF; 16 select t_error; //返回标识位的结果集; 17 END
hello,world~~~