【发布时间】:2019-10-13 16:34:31
【问题描述】:
我必须从表中生成 JSON。 问题是,我必须显示 NULL 值,但隐藏所有固定常量值。
例如,我有数据集#table。在 JSON 输出中,我想显示所有值,其中Value != 0。
删除行 (select Value from #table where cn = 'c') as 'c' 不是一种选择。
我该怎么做?
create table #table (
Value int,
cn nvarchar(1)
)
insert into #table
values
(null, 'a'),
(null, 'b'),
(0, 'c'),
(3, 'd'),
(3, 'f')
select
(select Value from #table where cn = 'a') as 'a',
(select Value from #table where cn = 'b') as 'b',
(select Value from #table where cn = 'c') as 'c',
(select Value from #table where cn = 'd') as 'd',
(select Value from #table where cn = 'f') as 'f'
FOR JSON PATH, INCLUDE_NULL_VALUES
预期的输出是:
[{"a":null,"b":null,"d":3,"f":3}]
【问题讨论】:
-
您想隐藏空值吗?
-
不,我想显示空值,但隐藏所有包含
0(如示例)或另一个固定(非空)值的值。 -
好的,我明白你的意思了:)
-
如果值有@987654327@,我们可以设置
null,否则你不会在JSON响应中隐藏这些列 -
用动态sql查看答案。
标签: sql json sql-server-2017