SQLSERVER 行转列 用逗号隔开

select DispatchList.cDLCode,DispatchLists.cSoCode from DispatchLists,DispatchList where DispatchList.DLID=DispatchLists.DLID and cDLCode='SN2025010610016'

----------------------------下面语句是行转列------------------------------------------------------------------------
select
l.cDLCode
,Subjects=stuff((select distinct ','+convert(varchar,DispatchLists.cSoCode)
from DispatchList,DispatchLists where DispatchList.DLID=DispatchLists.DLID and DispatchList.cDLCode=l.cDLCode
for xml path('')), 1, 1, '')
from
DispatchList l,DispatchLists where l.DLID=DispatchLists.DLID and cDLCode='SN2025010610016'
group by l.cDLCode

posted on 2025-01-08 15:49  八度空间  阅读(269)  评论(0)    收藏  举报

导航