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

前端 未结 4 978
[愿得一人]
[愿得一人] 2020-12-10 20:09

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 equa

相关标签:
4条回答
  • 2020-12-10 20:50

    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
    ;
    
    0 讨论(0)
  • 2020-12-10 20:59

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

    0 讨论(0)
  • 2020-12-10 20:59

    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.

    0 讨论(0)
  • 2020-12-10 21:05

    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!

    0 讨论(0)
提交回复
热议问题