Raising Error Conditions with MySQL SIGNAL / RESIGNAL Statements
http://www.mysqltutorial.org/mysql-signal-resignal/
Summary: in this tutorial, you will learn how to use SIGNAL and RESIGNAL statements to raise error conditions inside stored procedures.
MySQL SIGNAL statement
You use the SIGNAL statement to return an error or warning condition to the caller from a stored program e.g., stored procedure, stored function, triggeror event. The SIGNAL statement provides you with control over which information for returning such as value and messageSQLSTATE.
The following illustrates syntax of the SIGNAL statement:
|
1
2
3
|
SIGNAL SQLSTATE | condition_name;
SET condition_information_item_name_1 = value_1,
condition_information_item_name_1 = value_2, etc;
|
Following the SIGNAL keyword is a SQLSTATE value or a condition name declared by the DECLARE CONDITION statement. Notice that the SIGNAL statement must always specify a SQLSTATE value or a named condition that defined with an SQLSTATE value.
To provide the caller with information, you use the SET clause. If you want to return multiple condition information item names with values, you need to separate each name/value pair by a comma.
The condition_information_item_name can be MESSAGE_TEXT, MYSQL_ERRORNO,CURSOR_NAME , etc.
The following stored procedure adds an order line item into an existing sales order. It issues an error message if the order number does not exist.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
DELIMITER $$
CREATE PROCEDURE AddOrderItem(
in orderNo int,
in productCode varchar(45),
in qty int,
in price double,
in lineNo int )
BEGIN
DECLARE C INT;
SELECT COUNT(orderNumber) INTO C
FROM orders
WHERE orderNumber = orderNo;
-- check if orderNumber exists
IF(C != 1) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Order No not found in orders table';
END IF;
-- more code below
-- ...
END
|
First, it counts the orders with the input order number that we pass to the stored procedure.
Second, if the number of order is not 1, it raises an error with SQLSTATE 45000 along with an error message saying that order number does not exist in the orders table.
Notice that 45000 is a generic SQLSTATE value that illustrates an unhandled user-defined exception.
If we call the stored procedure AddOrderItem() and pass a nonexistent order number, we will get an error message.
|
1
|
CALL AddOrderItem(10,'S10_1678',1,95.7,1);
|

MySQL RESIGNAL statement
Besides the SIGNAL statement, MySQL also provides the RESIGNAL statement used to raise a warning or error condition.
The RESIGNAL statement is similar to SIGNAL statement in term of functionality and syntax, except that:
- You must use the
RESIGNALstatement within an error or warning handler, otherwise, you will get an error message saying that “RESIGNAL when handler is not active”. Notice that you can useSIGNALstatement anywhere inside a stored procedure. - You can omit all attributes of the
RESIGNALstatement, even theSQLSTATEvalue.
If you use the RESIGNAL statement alone, all attributes are the same as the ones passed to the condition handler.
The following stored procedure changes the error message before issuing it to the caller.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
DELIMITER $$
CREATE PROCEDURE Divide(IN numerator INT, IN denominator INT, OUT result double)
BEGIN
DECLARE division_by_zero CONDITION FOR SQLSTATE '22012';
DECLARE CONTINUE HANDLER FOR division_by_zero
RESIGNAL SET MESSAGE_TEXT = 'Division by zero / Denominator cannot be zero';
--
IF denominator = 0 THEN
SIGNAL division_by_zero;
ELSE
SET result := numerator / denominator;
END IF;
END
|
Let’s call the Divide() stored procedure.
|
1
|
CALL Divide(10,0,@result);
|

In this tutorial, we have shown you how to raise error conditions inside stored programs usingSIGNAL and RESIGNAL statements.
Raising Error Conditions with MySQL SIGNAL / RESIGNAL Statements的更多相关文章
- Conditions in bash scripting (if statements)
Shell中判断语句if中-z至-d的意思 - sunny_2015 - 博客园 https://www.cnblogs.com/coffy/p/5748292.html Conditions in ...
- 解决ERROR 2006 (HY000): MySQL server has gone away
刚把博客从百度云搬到腾讯云,发现文章少了几篇.当时在导入dump数据的时候,就曾经发现mysql提示: ERROR 2006 (HY000): MySQL server has gone away N ...
- Python pandas ERROR 2006 (HY000): MySQL server has gone away
之前在做python pandas大数据分析的时候,在将分析后的数据存入mysql的时候报ERROR 2006 (HY000): MySQL server has gone away 原因分析:在对百 ...
- 【linux】安裝 PHP时出现error: Cannot find MySQL header files
checking for specified location of the MySQL UNIX socket... no checking for MySQL UNIX socket locati ...
- SQLyog恢复数据库报错解决方法【Error Code: 2006 - MySQL server has gone away】
https://blog.csdn.net/niqinwen/article/details/8693044 导入数据库的时候 SQLyog 报错了 Error Code: 2006 – MySQL ...
- linux下安装php报错configure: error: Cannot find MySQL header files under /usr/include/mysql.
linux下安装php报错configure: error: Cannot find MySQL header files under /usr/include/mysql. 2013-03-04 1 ...
- ERROR 14856 --- [reate-882003853] com.alibaba.druid.pool.DruidDataSource : create connection error, url: jdbc:mysql://localhost:3306/xhb?useUnicode=true&characterEncoding=UTF-8, errorCode 1045, sta
ERROR 14856 --- [reate-882003853] com.alibaba.druid.pool.DruidDataSource : create connection error, ...
- 解决mysql跟php不在同一台机器上,编译安装php服务报错问题:configure: error: Cannot find MySQL header files under /application/mysql.
在编译安装php服务时报错: configure: error: Cannot find MySQL header files under /application/mysql. Note that ...
- MaxScale ERROR 2006 (HY000): MySQL server has gone away
Error: MaxScale cannot be run as root.Failed to write child process message!解决办法:# maxscale -f /etc/ ...
随机推荐
- telnet命令——连接服务器
Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户提供了在本地计算机上完成远程主机工作的能力.在终端使用者的电脑上使用telnet程序,用它连接 ...
- rabbitmq消息队列——"发布订阅"
三."发布订阅" 上一节的练习中我们创建了一个工作队列.队列中的每条消息都会被发送至一个工作进程.这节,我们将做些完全不同的事情--我们将发送单个消息发送至多个消费者.这种模式就是 ...
- 基于 flow.ci 实现 PHP 项目自动化持续集成
高效程序员的习惯之一--让开发流程自动化.Automating shapes smarter future. 这是一个关于如何快速实现 PHP 项目自动化持续集成的快速指导.无论你是否使用过持续集成, ...
- iOS-ARC项目使用非ARC文件 MRC项目使用ARC文件
SDK4.0引入了ARC,到现在已经好几年了,开始发现有很多项目会混合使用这两个方案.比如: 1.自己的旧项目没有使用ARC,但是引入的第三方库却是使用了ARC的. 2.自己的新项目使用了ARC,但是 ...
- iOS-旧项目中手动内存管理(MRC)转ARC
在ARC之前,iOS内存管理无论对资深级还是菜鸟级开发者来说都是一件很头疼的事.我参 加过几个使用手动内存管理的项目,印象最深刻的是一个地图类应用,由于应用本身就非常耗内存,当时为了解决内存泄露问题, ...
- Change Git Default Editor in Windows
On 32 bit Win OS: git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' - ...
- China Mobile 免流原理
用户添加包头之类(SSR软件),最后连接上了Server. 这个过程因为修改了数据包(混淆)以及服务器使用移动IP. 服务器得到信息后通过计费系统漏洞137 138端口等再返回给用户. 整个过程最重要 ...
- java之内部类详解
序言 有位小同学要我写一篇这个的总结,我说那好吧,那就动手写总结一下这个内部类的知识,感觉这个在面试中也会经常遇到,内部类.反射.集合.IO流.异常.多线程.泛型这些重要的基础知识大家都比较容易记不住 ...
- HTTP协议从入门到大牛,初识HTTP协议(学习笔记)
HTTP数据传输协议 当访问一个网页时,浏览器会向服务器发起一条HTTP请求,接着服务器会去寻找相应的资源,如果请求成功,就会把这个对象,对象类型,对象长度以及其他的信息放在HTTP响应中,发送给客户 ...
- 关于Git和Github你不知道的十件事
Git 和 GitHub都是非常强大的工具.即使你已经使用他们很长时间,你也很有可能不知道每个细节.我整理了Git和GitHub可能提高日常效率的10个常用技巧. GitHub 快捷键: t 和 w ...