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

家住魔仙堡 提交于 2019-11-28 11:40:57

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

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
;

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!

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.

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