Storing formula (equations) in database to be evaluated later (SQL Server 2005)

偶尔善良 提交于 2019-11-27 19:21:40

问题


I'm calculating linear regressions based on a data set. I do not know the regression model or number of parameters at compile-time.

I'm storing the regression equation in a SQL Server 2005 database as the string

y = 3x^2 + 2x // just an example

When I need to make a prediction, I grab the equation from the database, substitue x with the value I'm predicting, and use NCalc to evaluate the resulting string.

That method seems to work OK, but I'm wondering if there's a better way or a built-in feature of SQL Server that I've missed that would allow me to do these calculations on the database side.


回答1:


You could write a CLR stored procedure that still uses NCalc to do the calculation.




回答2:


I'd suggest putting it into a function along these lines. You can then call the function directly as well as having the ability to easily include the calculated value in view sets for reporting.

CREATE FUNCTION dbo.getRegression 
( @xvalue AS NUMERIC(18,2) --set the precision and scale as appropriate for your data
)
RETURNS NUMERIC(18,2)
    AS
    BEGIN
        DECLARE @yvalue as NUMERIC (18,2) 
        set @yvalue = POWER(2,(3*@xvalue)) + (2*@xvalue)
        RETURN @yvalue
    END
;



回答3:


This is not an answer but I don't have enough reputation to comment.

"You could write a CLR stored procedure that still uses NCalc to do the calculation."

You COULD do this, but remember that you can only add references to Sql Server Projects which can only reference other Sql Server Projects. So you COULD create a SqlServer Project and link all the files from the NCalc project and try to build that but then you will have to do the same with all the references of the NCalc project as well. Not all of which are open-source. I suppose you COULD use Reflector to decompile all these references and put those file in a SqlServer Project as well.

But if you did do all this and finally get your solution to build then you'd probably find out that you can only add the reference as an UNSAFE reference which would mean you'd have to start changing all sorts of SqlSever permissions...

At which point you'd probably give up.

What I'm trying to say is there is a lot more work here than the original answer suggests!




回答4:


In Sql Server, something like this Select 2+2 would return 4. So, you could have a stored procedure that reads the string out of the database and then builds another dynamic string let's call it (@SQLString) and run that query.

For example in this case the formula could be x + 2, then you build up a dynamic string based on that, and then call sp_executesql:

EXEC sp_executesql @SQLString

However, you should read this article on Dynamic SQL before you go down that road.

I believe what you are doing it just fine.



来源:https://stackoverflow.com/questions/9722782/storing-formula-equations-in-database-to-be-evaluated-later-sql-server-2005

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!