SQL Server中把子表数据转为json放到主表的某个字段

with tt as(
select ID,INFOTITLE,INFOCONTENT,CreateTime,CreateCode,IsRead ,data
FROM (SELECT ID,INFOTITLE,INFOCONTENT,CreateTime,CreateCode,IsRead ,(select '{"name":"'+thumbpath+'"},' from EDU_SCHOOLINFORMATIONIMGS where pid=s.infocode for xml path('')) as data ,ROW_NUMBER() over(order by IsRead asc) as rowNum FROM EDU_SCHOOLINFORMATION s where s.Deleted=0 and s.SchoolID=#SchoolID#) T
where T.rowNum between #StartPage# AND #EndPage#
)
select ID,INFOTITLE,INFOCONTENT,CreateTime,CreateCode,IsRead,'['+left(tt.data,len(tt.data)-1)+']' as INFOIMAGE from tt

更多技术交流+QQ:318617848
原文地址:https://www.cnblogs.com/huyaguang/p/5763730.html