问题
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 function 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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/7331543/how-do-i-eval-a-simple-math-formula-inside-a-mysql-stored-function