数据库行转列及逗号分隔转行

select TD_A.F_id,TD_A.F_UUIDId,TD_A.F_Name,
STUFF((select ','+F_Value from TD_Files left join TS_Enum on  F_TypeCode=F_Code and f_Key='AchievementsFilesTypeCode'
 where TD_Files.F_UUIDId=TD_A.F_UUIDId for xml path('')
) , 1 , 1 , '' )
 as LostInfo from TD_Achievements as TD_A where F_CreateTime<'2016-1-19' and f_state<4




DECLARE @s VARCHAR(100)='1,2,3,4,5,6'
SELECT t.c.value('.','int') AS col  from 
(SELECT CAST('<x>'+REPLACE(@s,',','</x><x>')+'</x>' AS XML ).query('.') AS name) AS a
CROSS APPLY a.name.nodes('/x') T(c)

你可能感兴趣的:(数据库)