补充:这个数学公式是用户通过前台配置的公式,而且可能会递归,例如:
KPI4=KPI3+90;    而KPI3=KPI1+20; KPI1的数据来源为输入型的,直接赋值为常量:50
这样一来:
KPI4=KPI1+20+90这个在程序中怎么写Sql语句呢?怎么将KPI4的值计算出来?

解决方案 »

  1.   

    出现死循环如何处理:
    KPI4=KPI1+20+90
    KPI1=KPI4+20+90
      

  2.   

    --> Title  : MSSQL計算表達式的值常見解決方案--> Author : wufeng4552--> Date   : 2009-11-25 08:15:08if object_id('[tb]') is not null drop table [tb]gocreate table [tb] (ID int,val nvarchar(14))insert into [tb]select 1,'1*5' union allselect 2,'1+5' union allselect 3,'1.0/5' union allselect 4,'1/5' union allselect 5,'2*5+3' union allselect 6,'(8-5)*3'--方法1 動態T-SQLdeclare @sql varchar(8000)set @sql=''select @sql=@sql+'select ID='+ltrim(ID)+', val='+val+' union all ' from tbset @sql=left(@sql,len(@sql)-10)exec(@sql)-->查詢結果/*ID          val----------- ---------------------------------------1           5.0000002           6.0000003           0.2000004           0.0000005           13.0000006           9.000000(6 個資料列受到影響)*/--方法2 游標declare @t table(ID int, val dec(18,2))declare @s varchar(50),@id int declare cur cursor for select ID,val from tbopen cur fetch next from cur into @id,@swhile @@fetch_status=0begin    insert @t exec('select '+@ID+','+@s)    fetch next from cur into @id,@sendclose curdeallocate cur select * from @t/*ID          val
    ----------- ---------------------------------------
    1           5.00
    2           6.00
    3           0.20
    4           0.00
    5           13.00
    6           9.00(6 個資料列受到影響)*/--方法3 函數if object_id('f_calc')is not null drop function f_calcgocreate function f_calc(@str varchar(1000)--要计 oa的表达 |?)returns sql_variantasbegindeclare @re sql_variantdeclare @err int,@src varchar(255),@desc varchar(255)declare @obj intexec @err=sp_oacreate 'MSScriptControl.ScriptControl',@obj outif @err<>0 goto lb_errexec @err=sp_oasetproperty @obj,'Language','vbscript'if @err<>0 goto lb_errexec @err=sp_oamethod @obj,'Eval',@re out,@strif @err=0 return(@re)lb_err:exec sp_oageterrorinfo NULL, @src out, @desc out declare @errb varbinary(4),@s varchar(20)set @errb=cast(@err as varbinary(4))exec master..xp_varbintohexstr @errb,@s outreturn(N'错誤号 '+@s+char(13)+N'错誤源: '+@src+char(13)+N'错误描述: '+@desc)endgo--以上方法要啟用OLE Automation Procedures 方法如下sp_configure 'show advanced options', 1;GORECONFIGURE;GOsp_configure 'Ole Automation Procedures', 1;GORECONFIGURE;GOselect ID,       dbo.f_calc(val)valfrom tb/*ID          val----------- --------------1           52           63           .24           .25           136           9(6 個資料列受到影響)*/本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wufeng4552/archive/2009/11/25/4868138.aspx
      

  3.   

    解决了 先把公式中的KPI值计算出来得出只含常量的公式如:90*123  
    然后用执行 select 90*123不过死循环倒是个问题哦 ,我还要去Check???求高手赐教