在 SQL Server 中执行列值 [英] Execute column values in SQL Server
本文介绍了在 SQL Server 中执行列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要对保存在 SQL 表中的值进行算术运算,例如,我在下一列中的值为 5*10 我想要 15
I need to do an arithmetic operation to the values saved in SQL table, for example, I have value as 5*10 in next column I want 15
EQUATION VALUE
2+5 7
6+8 14
根据我需要计算的等式.
Based on the equation I need to calculate the value.
推荐答案
正如您现在所知,SQL Server 没有 EVAL() 函数.但是,使用一点动态 SQL,这是可能的,但真的不推荐.
As you know by now, SQL Server does not have an EVAL() function. However, with a little dynamic SQL, it is possible, but really not recommended.
示例
Declare @YourTable Table (id int,[EQUATION] varchar(150))
Insert Into @YourTable Values
(1,'2+5')
,(2,'6+8')
,(3,'datediff(DAY,''2018-01-01'',getdate())') -- Added Just for Fun
Declare @SQL varchar(max) = Stuff((Select ',' + concat('(',ID,',',[EQUATION],')')
From @YourTable A
For XML Path (''))
,1,1,'')
Exec('Select * from (values ' + @SQL + ')A([ID],[Value])')
退货
ID Value
1 7
2 14
3 189
这篇关于在 SQL Server 中执行列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文