'Return'/Abort mySql stored procedure
I want to abort/quit/return from a stored procedure 开发者_JAVA技巧when a condition is not met. I'm not sure how to do it. Googling didn't help me much because:
- The version of MySql on our servers is 5.1 (<5.5 and hence no signal sqlstate).
- I don't want to put everything in if..then..else statements (there will be multiple levels of nesting in my case)
- I don't want to call an_invalid_procedure to raise an exception.
Are there alternatives?
Something wrong with....
...
BEGIN
<do stuff>
IF (<your condition>) THEN
<do more stuff>
END IF;
END$$
What I do in such cases is to:
Create table called
errors
with one column containing textual error messages and UNIQUE index on it. I fill the table with errors I want to support.When I need specific error to raised, I insert the error message into
errors
table. Since it's already there, I am violating a UNIQUE key so error is raised. In my application I can watch for MySQL error #1062 (that's the code for 'Duplicate value for key...' error and use regexp to extract my error message to show it to user/log it/whatever;
精彩评论