低版本SqlServer将查询结果转成JSON字符串

看看这个

第一步

第二步,去除最后一个逗号,整体添加[]

代码

select
	'{"ItemName":"' + s_ItemName + '"',
	',"Dosage":' + CONVERT(varchar, n_Dosage),
	',"DosageUnit":"' + s_DosageUnit + '"},'
from
	tAnesthesiaEvent
where
	s_ItemClass = 'B'
for xml path('')
--记得去掉最后一个逗号
select '[' + (select left(t.BLODOD_IN_List, LEN(t.BLODOD_IN_List) - 1) from (
				select (select '{"ItemName":"' + s_ItemName + '"',
							   ',"Dosage":' + CONVERT(varchar, n_Dosage),
							   ',"DosageUnit":"' + s_DosageUnit + '"},'
						from tAnesthesiaEvent
						where s_ItemClass = 'B'
						and n_DataType= 0
						--and s_OMNO = ''
						for xml path('')) as BLODOD_IN_List) t
			 ) + ']'
posted @ 2021-01-04 16:54  vvull  阅读(390)  评论(1编辑  收藏  举报