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

时间:2021-12-11 16:37:40

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

如何在MySQL存储过程中生成异常?例如:

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:

在MySQL中,我认为不可能在存储过程中抛出异常,但是我可以通过从不存在的表中进行选择来强制执行错误。例如:

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.

谢谢。

1 个解决方案

#1


14  

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).

由于MySQL 5.5,您可以使用信号和重信号处理错误。在此之前,MySQL中没有处理错误的方法。唯一的方法是运行一个错误的查询(例如插入到不存在的表中)。

#1


14  

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).

由于MySQL 5.5,您可以使用信号和重信号处理错误。在此之前,MySQL中没有处理错误的方法。唯一的方法是运行一个错误的查询(例如插入到不存在的表中)。