mysql 存储过程 抛异常,MySQL - 如何在存储过程中抛出异常?

本文探讨了在MySQL存储过程中如何生成异常。自MySQL 5.5版本起,可以通过使用SIGNAL和RESIGNAL语句来处理错误。在此之前,只能通过运行错误查询(如查询不存在的表)来触发异常。

How to generate an exception in the stored procedure in MySQL? For example:

CREATE PROCEDURE SALES()

BEGIN

STATEMENT...

STATEMENT...

STATEMENT...

IF (PRICE >= 500) THEN

/** THROWS AN EXCEPTION....

WHAT DO TO STOP THE PROCEDURE. **/

END IF;

STATEMENT...

STATEMENT...

STATEMENT...

END;

In MySQL I think there is no way to throw an exception in a stored procedure, but I can force an error by selecting from a non-existing table. For example:

IF (PRICE > 500) THEN

/*throw the error here*/

SELECT * FROM price_greater_than_500_in_throw_exception;

END IF;

Is there a more elegant way?

Thanks.

解决方案

Since MySQL 5.5 you can use SIGNAL and RESIGNAL for error handling. Prior to that there was no way to handle errors in MySQL. Only way is to run an erroneous query (for example inserting into non existing table).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值