开发者

How do I eval a simple math formula inside a MySQL stored _function_?

Inside my stored function I have :

formula := "(10+10 * 1000)/12";

(a simple math formula, with numbers only, dynamically created as a string)

How do I eval this, and return the result ?

I can't use EXECUTE (not possible inside a stored function) and if I make it a stored procedure and call it from a stored function, I get "Dynamic SQL is not allowed in stored fu开发者_高级运维nction or trigger" -as if I would have the eval directly inside the function. I need a stored function, and not a procedure, because I need to call it inside a SELECT statement.


I don't see what using the formula is buying you. If you're writing a stored procedure, type in the formula and forget the string.

I don't think it's in your interest to make the stored proc that dynamic where the formula being evaluated has to be changing from call to call.

If you must, you'll have to write a parser to break that string up into its constitutive parts, create a parse tree, and then walk the tree to evaluate it. It's not a trivial problem. I'd rethink this.


Apparently there is no solution to this. I have applied a "paintfull" workaround in PHP, which I will not display here as it is not the subject of the question.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜