MySQL存储过程的异常处理方法
网络编程 2021-07-05 15:28www.168986.cn编程入门
这篇文章主要介绍了MySQL存储过程的异常处理方法,可实现有效调试MySQL存储过程处理结果的功能,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了MySQL存储过程的异常处理方法。分享给大家供大家参考。具体如下
mysql> mysql> delimiter $$ mysql> mysql> CREATE PROCEDURE myProc -> (p_first_name VARCHAR(30), -> p_last_name VARCHAR(30), -> p_city VARCHAR(30), -> p_description VARCHAR(30), -> OUT p_sqlcode INT, -> OUT p_status_message VARCHAR(100)) -> BEGIN -> -> / START Declare Conditions / -> -> DECLARE duplicate_key CONDITION FOR 1062; -> DECLARE foreign_key_violated CONDITION FOR 1216; -> -> / END Declare Conditions / -> -> / START Declare variables and cursors / -> -> DECLARE l_manager_id INT; -> -> DECLARE csr_mgr_id CURSOR FOR -> SELECT id -> FROM employee -> WHERE first_name=p_first_name -> AND last_name=p_last_name; -> -> / END Declare variables and cursors / -> -> / START Declare Exception Handlers / -> -> DECLARE CONTINUE HANDLER FOR duplicate_key -> BEGIN -> SET p_sqlcode=1052; -> SET p_status_message='Duplicate key error'; -> END; -> -> DECLARE CONTINUE HANDLER FOR foreign_key_violated -> BEGIN -> SET p_sqlcode=1216; -> SET p_status_message='Foreign key violated'; -> END; -> -> DECLARE CONTINUE HANDLER FOR not FOUND -> BEGIN -> SET p_sqlcode=1329; -> SET p_status_message='No record found'; -> END; -> -> / END Declare Exception Handlers / -> -> / START Execution / -> -> SET p_sqlcode=0; -> OPEN csr_mgr_id; -> FETCH csr_mgr_id INTO l_manager_id; -> -> IF p_sqlcode<>0 THEN / Failed to get manager id/ -> SET p_status_message=CONCAT(p_status_message,' when fetching manager id'); -> ELSE -> INSERT INTO employee (first_name,id,city) -> VALUES(p_first_name,l_manager_id,p_city); -> -> IF p_sqlcode<>0 THEN / Failed to insert new department / -> SET p_status_message=CONCAT(p_status_message, -> ' when inserting new department'); -> END IF; -> END IF; -> -> CLOSE csr_mgr_id; -> -> / END Execution / -> -> END$$ Query OK, 0 rows affected (0.02 sec) mysql> mysql> delimiter ; mysql> set @myCode = 0; Query OK, 0 rows affected (0.00 sec) mysql> set @myMessage = 0; Query OK, 0 rows affected (0.00 sec) mysql> mysql> call myProc('Jason','Martin','New City','New Description',@myCode,@myMessage); Query OK, 1 row affected (0.00 sec) mysql> mysql> select @myCode, @myMessage; +---------+------------+ | @myCode | @myMessage | +---------+------------+ | 0 | NULL | +---------+------------+ 1 row in set (0.00 sec) mysql> mysql> drop procedure myProc; Query OK, 0 rows affected (0.00 sec)
希望本文所述对大家的MySQL数据库程序设计有所帮助。
编程语言
- 甘肃哪有关键词排名优化购买方式有哪些
- 甘肃SEO如何做网站优化
- 河南seo关键词优化怎么做电话营销
- 北京SEO优化如何做QQ群营销
- 来宾百度关键词排名:提升您网站曝光率的关键
- 卢龙关键词优化:提升您网站排名的策略与技巧
- 山东网站优化的注意事项有哪些
- 四川整站优化怎样提升在搜索引擎中的排名
- 疏附整站优化:提升网站性能与用户体验的全新
- 海南seo主要做什么工作售后服务要做到哪些
- 荣昌百度网站优化:提升您网站的搜索引擎排名
- 河北seo网站排名关键词优化如何做SEO
- 江西优化关键词排名推广售后保障一般有哪些
- 古浪SEO优化:提升你的网站可见性
- 西藏网站排名优化怎么把网站排名在百度首页
- 如何提升阳东百度快照排名:详尽指南