且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

mysql/oracle存储的数学公式

更新时间:2023-02-13 20:57:43

我认为您的意思是希望数据库解析公式字符串.例如,对于Oracle,您可以

I think what you're saying is you want to have the database parse the formula string. For example, for Oracle you could

  • 在表中添加一列以包含结果
  • 运行一条更新语句,该语句将使用表中列的值和公式的文本

  • Add a column to the table to contain the result
  • Run an update statement which would call a PL/SQL function with the values of the columns in the table and the text of the formula

更新{table}设置公式_结果= fn_calc_result(col1,col2,公式_列);

update {table} set formula_result = fn_calc_result (col1, col2, formula_column);

PL/SQL函数将通过用这些列的实际值替换"col1"和"col2"等来创建一个字符串.只要公式始终一致,您就可以使用常规表达式来执行此操作.

The PL/SQL function would create a string by replacing the "col1" and "col2" and so forth with the actual values of those columns. You can do that with regular expresions, as long as the formulas are consistently written.

然后使用

execute immediate 'select '||{formula}||' from dual' into v_return;
return v_return;

计算结果并返回.

当然,您也可以编写自己的解析器.如果您决定采用这种方式,请不要忘记处理操作优先级,括号等.

Of course, you could also write your own parser. If you decide to go that way, don't forget to handle operation precedence, parentheses, and so forth.