Home > Database > Mysql Tutorial > body text

Mysql series (11) exception handling

黄舟
Release: 2017-01-22 16:57:35
Original
1172 people have browsed it

Mysql Series (11) Exception Handling

DROP PROCEDURE IF EXISTS `SP_MODEL`;
DELIMITER ;;
CREATE PROCEDURE `SP_MODEL`(IN V_TYPE INT)
BEGIN
/**********存储过程模版,结合了·返回自定义错误信息·错误退出··事物回滚·的功能***********/
DECLARE V_TEST INT DEFAULT 0;
DECLARE V_ERR_NO INT DEFAULT 0;
DECLARE V_ERR_MSG VARCHAR(100) DEFAULT '执行成功';
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION,SQLWARNING,NOT FOUND SET V_ERR_NO=1;
-- CREATE TABLE `test_error` (
-- `a` int(11) NOT NULL,
-- `b` int(11) DEFAULT NULL,
-- PRIMARY KEY (`a`)
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- TRUNCATE TABLE TEST_ERROR;
-- CALL SP_MODEL(1);
-- SELECT * FROM TEST_ERROR;
START TRANSACTION;
Label:-- 执行过程中出现错误则跳出此Label
BEGIN
IF V_TYPE=1 THEN
INSERT INTO TEST_ERROR(a,b) VALUES(1,1);
INSERT INTO TEST_ERROR(a,b) VALUES(1,1);
IF V_ERR_NO=1 THEN
SET V_ERR_MSG='您已经插过啦,别再重复插~';
-- LEAVE Label;
END IF;
SET V_TEST=1;
INSERT INTO TEST_ERROR(a,b) VALUES(3,'a');
IF V_ERR_NO=1 THEN
SET V_ERR_MSG='尺寸不对,插不进来~';
LEAVE Label;
END IF;
INSERT INTO TEST_ERROR(a,b) VALUES(4,1);
SET V_TEST=2;
ELSE
SET V_ERR_MSG='传入参数V_TYPE不正确';
SET V_ERR_NO=1; -- 直接设置此变量值为1,主动抛出异常
LEAVE Label;
END IF;
END Label;
IF V_ERR_NO=1 THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
SELECT V_ERR_NO AS ERR_NO,V_ERR_MSG AS ERR_MSG, V_TEST AS TEST; -- 返回执行结果
END
;;
DELIMITER ;
Copy after login

The above is the content of Mysql Series (11) Exception Handling. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!