我有一个SQL Server列,其数据如下:
formula amount ficheno
-----------------------------
100*444 100 6555
10*698 698 6555
我已经尝试过在SQL Server上转换varchar
来加倍获取错误。
需要获得公式字段到结果字段...
您可以使用动态SQL计算表达式:
declare @sql nvarchar(max) = (
select string_agg(cast(
'select ' + formula + ', ' + str(amount) + ', ' + str(ficheno)
as nvarchar(max)), ';')
from YourTable
);
declare @temp table (id int, amount int, ficheno int);
insert @temp exec(@sql);
select * from @temp;
-->
id amount ficheno
44400 100 6555
6980 698 6555
我已经按照您的查询并成功了,非常感谢@Andomar。已审核的代码可用于sql server <2017 with stuff功能。如果有人在寻找答案;
declare @sql nvarchar(max) = (
SELECT STUFF( (SELECT ' select ' + str([LOGICALREF]) + ', ' + str([LOGICALREFI]) + ', ' + REPLACE( Formül,',','.') +';'
from BM_211_URETIM_PLANLANAN t
for xml path ('')
), 1, 1, '' )
)
declare @temp table ( [LOGICALREF] int, [LOGICALREFI] int, [Planlanan Miktar] FLOAT);
insert @temp exec(@sql);