急急:表中的某个字段是数学公式字符串 应该怎么计算其值

maysaber 2011-05-16 04:12:33
补充:这个数学公式是用户通过前台配置的公式,而且可能会递归,例如:
KPI4=KPI3+90; 而KPI3=KPI1+20; KPI1的数据来源为输入型的,直接赋值为常量:50
这样一来:
KPI4=KPI1+20+90

这个在程序中怎么写Sql语句呢?怎么将KPI4的值计算出来?
...全文
231 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
maysaber 2011-05-19
  • 打赏
  • 举报
回复
解决了 先把公式中的KPI值计算出来得出只含常量的公式如:90*123
然后用执行 select 90*123

不过死循环倒是个问题哦 ,我还要去Check???求高手赐教
--小F-- 2011-05-16
  • 打赏
  • 举报
回复
--> Title  : MSSQL計算表達式的值常見解決方案

--> Author : wufeng4552

--> Date : 2009-11-25 08:15:08

if object_id('[tb]') is not null drop table [tb]

go

create table [tb] (ID int,val nvarchar(14))

insert into [tb]

select 1,'1*5' union all

select 2,'1+5' union all

select 3,'1.0/5' union all

select 4,'1/5' union all

select 5,'2*5+3' union all

select 6,'(8-5)*3'

--方法1 動態T-SQL

declare @sql varchar(8000)

set @sql=''

select @sql=@sql+'select ID='+ltrim(ID)+', val='+val+' union all ' from tb

set @sql=left(@sql,len(@sql)-10)

exec(@sql)

-->查詢結果

/*

ID val

----------- ---------------------------------------

1 5.000000

2 6.000000

3 0.200000

4 0.000000

5 13.000000

6 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 tb

open cur

fetch next from cur into @id,@s

while @@fetch_status=0

begin

insert @t exec('select '+@ID+','+@s)

fetch next from cur into @id,@s

end

close cur

deallocate 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_calc

go

create function f_calc(

@str varchar(1000)--要计 oa的表达 |?

)returns sql_variant

as

begin

declare @re sql_variant

declare @err int,@src varchar(255),@desc varchar(255)

declare @obj int

exec @err=sp_oacreate 'MSScriptControl.ScriptControl',@obj out

if @err<>0 goto lb_err

exec @err=sp_oasetproperty @obj,'Language','vbscript'

if @err<>0 goto lb_err

exec @err=sp_oamethod @obj,'Eval',@re out,@str

if @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 out

return(N'错誤号 '+@s+char(13)+N'错誤源: '+@src+char(13)+N'错误描述: '+@desc)

end

go

--以上方法要啟用OLE Automation Procedures 方法如下

sp_configure 'show advanced options', 1;

GO

RECONFIGURE;

GO

sp_configure 'Ole Automation Procedures', 1;

GO

RECONFIGURE;

GO

select ID,

dbo.f_calc(val)val

from tb

/*

ID val

----------- --------------

1 5

2 6

3 .2

4 .2

5 13

6 9

(6 個資料列受到影響)

*/



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wufeng4552/archive/2009/11/25/4868138.aspx
maysaber 2011-05-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 m7777 的回复:]
出现死循环如何处理:
KPI4=KPI1+20+90
KPI1=KPI4+20+90
[/Quote]

这也是个问题!
maysaber 2011-05-16
  • 打赏
  • 举报
回复
对了:还有括号的匹配问题,这个真的是让我头大了,请高手赐教!!谢谢
槑党灬穆奇奇 2011-05-16
  • 打赏
  • 举报
回复
出现死循环如何处理:
KPI4=KPI1+20+90
KPI1=KPI4+20+90


槑党灬穆奇奇 2011-05-16
  • 打赏
  • 举报
回复
拼字符串 递归

22,301

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧