如何在不使用CONCAT的情况下使用其他列的值组合更新列

时间:2014-01-03 14:15:02

标签: sql-server-2008 sql-update

我有一个表,其中包含一列,它是来自其他列的逗号分隔值的组合,例如

    update K2_Data 
set Long_Desc = (PremiseRef,',',BuildRef,',',BlockRef,',',Location,',',ItemRef,',',Quant,',',Life)
where k2_ref = 431578

我已尝试过此网站的各种示例,包括'+'运算符,但无法使其正常工作。

    update K2_Data 
set Long_Desc = (PremiseRef+','+BuildRef+','+BlockRef+','+Location+','+ItemRef+','+Quant+','+Life)
where k2_ref = 431578

非常感谢任何帮助,

由于

2 个答案:

答案 0 :(得分:0)

将每个非varchar列转换为varchar,然后使用'+'运算符进行连接。应该这样做。

编辑后

update K2_Data 
set Long_Desc = (cast(PremiseRef as varchar) +','+BuildRef+','+BlockRef+','+Location+','+cast(ItemRef as varchar)+','+cast(Quant as varchar)+','+Life)
where k2_ref = 431578

第二次编辑后

update K2_Data 
set Long_Desc = (cast(PremiseRef as varchar) +','+cast(BuildRef as varchar)+','+cast(BlockRef as varchar)+','+cast(Location as varchar)+','+cast(ItemRef as varchar)+','+cast(Quant as varchar)+','+cast(Life as varchar))
where k2_ref = 431578

答案 1 :(得分:0)

您无法将intnvarchar连接起来。您必须按如下方式将整数转换为varchar:

update K2_Data 
set Long_Desc (CAST(PremiseRef AS VARCHAR(10)+','+ BuildRef+','+BlockRef+','+Location+','+CAST(ItemRef AS VARCHAR(10)+','+CAST(Quant AS VARCHAR(10)+','+CAST(Life AS VARCHAR(10))
where k2_ref = 431578