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

posted @ 2016-08-12 09:40  blackhole1  阅读(508)  评论(0编辑  收藏  举报