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 functiontriggeror 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_TEXTMYSQL_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 RESIGNAL  statement 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 use SIGNAL  statement anywhere inside a stored procedure.
  • You can omit all attributes of the RESIGNAL statement, even the SQLSTATE value.

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.

时间: 2024-11-10 11:53:01

Raising Error Conditions with MySQL SIGNAL / RESIGNAL Statements的相关文章

DATABASE CONNECTION ERROR (1): THE MYSQL ADAPTER 'MYSQLI' IS NOT AVAILABLE.解决办法

网站迁移之后遇到:Database connection error (1): The MySQL adapter 'mysqli' is not available.这个问题,我一开始以为是我配置的参数错了呢!然后回去检查,检查好几遍我的数据库账号和密码都没错,但是为什么就是连接不上数据库呢?? 网上有人说是"可能有你安装wamp或者xamp之后,sqlserver的服务没有启动,你可以到系统服务菜单里看看这个是否自动启动."但是我问过空间商,他们说默认都是开启的,然后他们也检查了数

Error Code: 2006 - MySQL 鏈嶅姟鍣ㄥ凡绂荤嚎

将sql文件导入到mysql时候,就一直报这个错误.我试过网上各种方法都行不通.最后将下面一句执行了一下就可以了,而且没有重启mysql. SET GLOBAL max_allowed_packet=67108864; Error Code: 2006 - MySQL 鏈嶅姟鍣ㄥ凡绂荤嚎,布布扣,bubuko.com

mysql 数据库还原出错ERROR:Unknown command '\' mysql中断

其实造成这个问题的原因还是由于编码的问题,网站数据库设置的是gbk 的,mysql默认是gbk:但是在导出数据的时候导出了utf8的sql文件,不管我如何重新导入,在连接数据库后使用set names utf8:都不行,如果用gbk导入一是一直是乱码也会出错,在网上找了好久刚好有个和我一样的问题 .她的解决方法如下 在连接服务器端时命令中指定编码,如下形式 >mysql -uroot -pxxxxxx --default-character-set=utf8 我试了不行,出错,提示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 15:34wdjhz | 分类:服务器软件 | 浏览5318次 configure: error: Cannot find MySQL header files under /usr/include/mysql.Note that the MySQL client library is not bun

SQLyog恢复数据库报错解决方法【Error Code: 2006 - MySQL server has gone away】

https://blog.csdn.net/niqinwen/article/details/8693044 导入数据库的时候 SQLyog 报错了 Error Code: 2006 – MySQL server has gone away 搜了下,说是max_allowed_packet (MySQL的一个参数)设置的值不够大. 那我改下就行了 嘿嘿 In Windows: In the MySQL server installation directory,in my.ini file, a

error: index-pack died of signal fatal: index-pack failed【Git】

error: index-pack died of signal fatal: index-pack failed 环境: 克隆Linux源码时发生错误 git clone https://github.com/torvalds/linux.git error: index-pack died of signal fatal: index-pack failed google云Compute Engine Memory: 0.6GB内存 Linux版本: Debian GNU/Linux 9.5

解决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 the MySQL client library is not bundled anymore! 前边搭建lnmp环境时,是把mysql和php安装在了同一台机器上,编译php的时候,需要通过参数 --with-mysql来指定mysql的安装路径,但在生产环境中,通常php和mysql是不在同一台

[故障解决]Mysql爆出ERROR 2006 (HY000): MySQL server has gone away的错误怎么办?

有时候,mysql会爆出MySQL server has gone away,比如像这样: 目前我遇到的这样的情况有三种解决方法: 1)需要添加白名单,这种情况在云服务器上比较常见.在云商后台里把对应机器添加到rds白名单即可: 2)调整一下服务器的时间,一般来说这样的情况在虚拟机上常见,检查一下虚拟机时间是否与宿主机时间不同,如果不同,请#yum -y -q install ntp,然后#ntpdate -b  time5.aliyun.co. 3)导入一个比较大的sql文件也会出现这样的错误

ERROR 2002 (HY000):mysql 启动常见错误

解决办法:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) vim  /etc/my.cnf 在最后新加一个段,因为在编译安装的时候,路径写的是sock=/tmp/mysql.sock  ,但是启动时,系统回去默认找var/lib/mysql/mysql.sock, 所以就新加个字段指定实际路径 [mysql] socket=/tmp/mys