create procedure sp_CalcExpression
@Expression varchar(8000)
,@ decimal(10,2) out
as
declare @sql nvarchar(4000)
set @sql = N'set @ = ' + @Expression
exec SP_EXECUTESQL @sql ,N'@ decimal(10,2) out', @ out
select @
--==============================
--调用示例:
declare @x decimal(10,2)
exec sp_CalcExpression '(100 + 2.1)*0.01', @x out
select @x
--==============================
--相关帖子:
http://community.csdn.net/Expert/TopicView.asp?id=3619946
http://community.csdn.net/Expert/TopicView.asp?id=3569862
--================
--下面是支持 SQL 其他内置函数的
create procedure sp_ExecExpression
@Expression varchar(8000)
,@ varchar(8000) out
as
declare @sql nvarchar(4000)
set @sql = N'set @ = ' + @Expression
exec SP_EXECUTESQL @sql ,N'@ varchar(8000) out', @ out
select @
--调用示例:
declare @x varchar(100)
exec sp_ExecExpression 'left(''aBcD'',2)',@x