要计算为数字的字符串表达式 [英] String Expression to be evaluated to number

查看:33
本文介绍了要计算为数字的字符串表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个 TSQL 用户定义函数,它将接受一个字符串并返回一个数字.

I need to write a TSQL user defined function which will accept a string and return a number.

我会调用像 dbo.EvaluateExpression('10*4.5*0.5') 这样的函数应该返回数字 22.5

I will call the function like dbo.EvaluateExpression('10*4.5*0.5') should return the number 22.5

谁能帮我写这个函数EvaluateExpression.

目前我正在使用我需要避免的 CLR 函数.

Currently I am using CLR function which I need to avoid.

编辑 1

我知道这可以使用存储过程来完成,但我想在一些语句中调用这个函数,例如:select 10* dbo.EvaluateExpression('10*4.5*0.5')

I know this can be done using stored procedure, but I want to call this function in some statements ex: select 10* dbo.EvaluateExpression('10*4.5*0.5')

我还有大约 400,000 个这样的公式需要评估.

Also I have around 400,000 formulas like this to be evaluated.

Edit2

我知道我们可以在函数内部使用 osql.exe 来做到这一点,正如 here.但是由于权限设置,我也不能使用这个.

I know we can do it using osql.exe inside function as explained here. But due to permission settings, I can not use this also.

推荐答案

我认为这在用户定义的函数中是不可能的.

I don't think that is possible in a user defined function.

您可以在存储过程中执行此操作,例如:

You could do it in a stored procedure, like:

declare @calc varchar(max)
set @calc = '10*4.5*0.5'

declare @sql nvarchar(max)
declare @result float
set @sql = N'set @result = ' + @calc
exec sp_executesql @sql, N'@result float output', @result out
select @result

但是动态 SQL,如 execsp_executesql,在用户定义的函数中是不允许的.

But dynamic SQL, like exec or sp_executesql, is not allowed in user defined functions.

这篇关于要计算为数字的字符串表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆