以前写的是 sql server,对mysql事物的写法不是很清楚,现整理了一个例子
delimiter // /* CALL sp_TranTest('0'); */ DROP PROCEDURE IF EXISTS sp_TranTest; CREATE PROCEDURE sp_TranTest( inDD VARCHAR(20) ) NOT DETERMINISTIC-- DETERMINISTIC:表示存储过程对同样的输入参数产生相同的结果;NOT DETERMINISTIC,则表示会产生不确定的结果(默认) SQL SECURITY DEFINER COMMENT'测试事物' Begin declare _err int default 0; -- 是事先定义程序执行过程中可能遇到的问题。并且可以在处理程序中定义解决这些问题的办法。这种方式可以提前预测可能出现的问题,并提出解决方法。 DECLARE CONTINUE HANDLER FOR SQLEXCEPTION,SQLWARNING,NOT FOUND set _err=1; start transaction ; INSERT INTO TranTestTable(DD) VALUES('00'); if(inDD='0') THEN SIGNAL SQLSTATE 'HY000' SET MESSAGE_TEXT = '错误'; END IF; -- rollback; IF(_err=1) THEN rollback; ELSE commit; END IF; END // delimiter ;