Sqlserver Stuff+for xml+values 记录下,将多列数据有数据的合并为一个单元格

Sqlserver Stuff+for xml+values 记录下,将多列数据有数据的合并为一个单元格
select InspectionRecordId,stuff( ( 
select  ','+col_name+':'+cast(col_value as varchar(max)) from
(   values('123',PrintingDefectQty),('456',VacuumFormingDefectQty)
) AS cols(col_name, col_value)  where  col_value >0
FOR XML PATH('')
),1,1,'')as  MergeData FROM [QI_T_DefectRecords]

image

 

image

 

image

将数据中 几十列 其中有错误的 列筛选出来  合并在一起