开发者

'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:

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

  2. 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;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜