MySQL 存储过程
创建数据表
DROP TABLE IF EXISTS `ims_z_my_info`;
CREATE TABLE `ims_z_my_info` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`balance` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of ims_z_my_info
-- ----------------------------
INSERT INTO `ims_z_my_info` VALUES ('1', '11', '111');
INSERT INTO `ims_z_my_info` VALUES ('2', '22', '222');
INSERT INTO `ims_z_my_info` VALUES ('3', '33', '333');
创建存储过程
DROP PROCEDURE IF EXISTS proc2;
delimiter //
CREATE PROCEDURE proc2(IN cid CHAR(18), OUT num INT)
BEGIN
#事务处理标志
DECLARE t_error INTEGER DEFAULT 0;
#申明事务处理错误标志
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, sqlwarning, not found SET t_error=1;
#开始事务
START TRANSACTION;
DELETE FROM ims_z_my_info WHERE iddd = cid;
SELECT count(id) INTO num FROM ims_z_my_info;
IF t_error = 1 THEN
#订单回滚
ROLLBACK;
# 捕捉到sql语句错误,插入一条新纪录。
INSERT INTO ims_z_my_info VALUES('22', 'xxxx', '22334545');
ELSE
#提交
COMMIT;
END IF;
END //
delimiter ;
执行语句
SELECT * FROM ims_z_my_info;
call proc2(1, @num)
SELECT @num