MySQL - How to throw exception in stored procedure?

两盒软妹~` 提交于 2020-01-01 01:30:34

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/9131379/mysql-how-to-throw-exception-in-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!