mysql 存储过程 事务处理 (转)
BEGIN
DECLARE t_error INTEGER DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;
START TRANSACTION;
#这边放sql语句,涉及到的表必须都为InnoDB
IF t_error = 1
THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
END
http://www.cnblogs.com/bossikill/p/3682839.html
DROP PROCEDURE IF EXISTS test_sp1
CREATE PROCEDURE test_sp1( )
BEGIN
DECLARE t_error INTEGER DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;
START TRANSACTION;
INSERT INTO test VALUES(NULL, 'test sql 001');
INSERT INTO test VALUES('1', 'test sql 002');
IF t_error = 1 THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
END
返回执行状态,即是提交了还是回滚了:
DROP PROCEDURE IF EXISTS test_sp1
CREATE PROCEDURE test_sp1( )
BEGIN
DECLARE t_error INTEGER DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;
START TRANSACTION;
INSERT INTO test VALUES(NULL, 'test sql 001');
INSERT INTO test VALUES('1', 'test sql 002');
IF t_error = 1 THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
select t_error; //返回标识位的结果集;
END