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/ ...
随机推荐
- 关于js内部运行机制的一本好书
读<单页Web应用一书>,第二章讲了js内部运行机制,感觉棒极了.之前读<你不知道的js>,看的云里雾里,似懂非懂.没想到单页Web一书将此内容讲的如此通俗易懂,好多困惑已久的 ...
- 腾讯Ubuntu云虚拟主机设置ftp服务器
刚申请了免费的腾讯云主机, 发现还要想办法自己的服务器代码传到云主机上 在网上搜了很多方法介绍, 照着设置完后都无法正常连接 最后半夜尿醒来睡不着找到一篇站内文章, 提到必须注释掉一行代码 这个是其他 ...
- Oracle函数脚本记录
--内置函数 --聚合函数 返回单个值 '; --count()记录条数 select sum(degree) from score t; --sum(degree)求成绩总和 select avg( ...
- 大叔也说Xamarin~Android篇~为HttpClient共享Session,android与api的session共享机制
回到目录 杂谈 在进行android进行开发时,我们的数据一般通过接口来获收,这里指的接口泛指web api,webservice,wcf,web应用程序等:它们做为服务端与数据库进行直接通讯,而AP ...
- eclipse提交项目到github
1.在https://github.com new repository 2.在eclipse中new project 比如:Test项目 3.右击"Test"->Te ...
- Python - 动手写个ORM
Python - 动手写个ORM 任务: 模拟简单的ORM - Object Relational Mapping 为model添加create方法 代码很简单,直接上 字段类型类 class Fie ...
- 将Windows系统编译的.NET Core程序发布到Ubuntu系统
在可移植方面.NET Core应用程序分为两种,Portable application(便捷,需要目标机器安装.NET Core Runtime)和Self-contained applicatio ...
- .NET面试题解析(03)-string与字符串操作
系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 字符串可以说是C#开发中最常用的类型了,也是对系统性能影响很关键的类型,熟练掌握字符串的操作非常重要. 常 ...
- Android入门(十九)WebView
原文链接:http://www.orlion.ga/676/ WebView可以在自己的应用程序中嵌入一个浏览器来展示网页. 创建一个项目WebViewDemo,修改activity_main.xml ...
- 【目录】微软Infer.NET机器学习组件文章目录
本博客所有文章分类的总目录链接:http://www.cnblogs.com/asxinyu/p/4288836.html 1.微软Infer.NET机器学习组件目录 1. Infer.NET连载(一 ...