经过大量的搜索和拼接使用围绕Web的FOR XML和.nodes()命令转换结果集的非常出色的技术,我能够创建这个单一的查询(而不是存储过程)合理地执行很好地将任意SQL查询转换为JSON数组.
该查询将每个数据行编码为具有前导逗号的单个JSON对象.数据行用括号括起来,然后整个结果集将被导出到文件中.
我想看看有没有人能看到改善其性能的方法?
这是带有示例表的查询:
declare @xd table (col1 varchar(max), col2 int, col3 real, colNull int)
insert into @xd
select '', null, null, null
UNION ALL select 'ItemA', 123, 123.123, null
UNION ALL select 'ItemB', 456, 456.456, null
UNION ALL select '7890', 789, 789.789, null
select '[{}'
UNION ALL
select ',{' + STUFF((
(select ','
+ '"' + r.value('local-name(.)', 'varchar(max)') + '":'
+ case when r.value('./@xsi:nil', 'varchar(max)') = 'true' then 'null'
when isnumeric(r.value('.', 'varchar(max)')) = 1
then r.value('.', 'varchar(max)')
else …Run Code Online (Sandbox Code Playgroud)