将密钥获取到 SQL Server 2017 中的 JSON 列
Get keys into JSON column in SQL Server 2017
我在 SQL Server 2017 的 NVARCHAR(MAX)
类型的列中有这个 Json:
{
"coreTimes": {
"TMP_CLINICAL_TIMES_ANESTHESIOLOGY_OUT_DATETIME": {
"value": 1
},
"TMP_CLINICAL_TIMES_ANESTHESIOLOGY_IN_DATETIME": {
"value": 2
},
"TMP_CLINICAL_TIMES_SURGICAL_OUT_DATETIME": {
"value": 3
},
"TMP_CLINICAL_TIMES_ROOM_IN_DATETIME": {
"value": 4
},
"TMP_CLINICAL_TIMES_ROOM_OUT_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_BLOCK_OUT_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_BLOCK_IN_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_SURGICAL_IN_DATETIME": {
"value": null
}
}
}
我需要这个结果:
Column
{"value":1}
{"value":2}
{"value":3}
{"value":4}
{"value":null}
{"value":null}
{"value":null}
如果 json 属性可以动态更改(键 'coreTimes' 是固定的),我可以使用什么 SQL Server 2017 函数来获得此结果?
我在 Oracle 中使用过:
SELECT res.*
FROM sopinterventionsaux ,
JSON_TABLE ( operating_times, '$.coreTimes.*'
COLUMNS (
value VARCHAR2 ( 2000 ) FORMAT JSON PATH '$'
)
) res
在 Postgre 中SQL:
select value from table, jsonb_each(column-> 'coreTimes')
然后在 SQL 服务器中?
如果我理解正确,您需要使用 OPENJSON() 和默认架构来解析输入 JSON
。在这种情况下 OPENJSON()
returns 具有列 key
、value
和 type
:
的 table
Table:
DECLARE @json nvarchar(max) = N'{
"coreTimes": {
"TMP_CLINICAL_TIMES_ANESTHESIOLOGY_OUT_DATETIME": {
"value": 1
},
"TMP_CLINICAL_TIMES_ANESTHESIOLOGY_IN_DATETIME": {
"value": 2
},
"TMP_CLINICAL_TIMES_SURGICAL_OUT_DATETIME": {
"value": 3
},
"TMP_CLINICAL_TIMES_ROOM_IN_DATETIME": {
"value": 4
},
"TMP_CLINICAL_TIMES_ROOM_OUT_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_BLOCK_OUT_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_BLOCK_IN_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_SURGICAL_IN_DATETIME": {
"value": null
}
}
}'
CREATE TABLE Data (JsonData nvarchar(max))
INSERT INTO Data (JsonData) VALUES (@json)
声明:
SELECT j.[value]
FROM Data d
CROSS APPLY OPENJSON(d.JsonData, '$.coreTimes') j
结果:
---------------
value
---------------
{"value": 1}
{"value": 2}
{"value": 3}
{"value": 4}
{"value": null}
{"value": null}
{"value": null}
{"value": null}
如果您想获得 value
键的准确值,您需要将 OPENJSON()
与显式模式(带有列定义)和附加的 APPLY
运算符一起使用。
声明:
SELECT j2.[value]
FROM Data d
CROSS APPLY OPENJSON(d.JsonData, '$.coreTimes') j1
CROSS APPLY OPENJSON(j1.[value]) WITH ([value] int '$.value') j2
结果:
-----
value
-----
1
2
3
4
NULL
NULL
NULL
NULL
请尝试以下操作:
declare @j nvarchar(max) = '{
"coreTimes": {
"TMP_CLINICAL_TIMES_ANESTHESIOLOGY_OUT_DATETIME": {
"value": 1
},
"TMP_CLINICAL_TIMES_ANESTHESIOLOGY_IN_DATETIME": {
"value": 2
},
"TMP_CLINICAL_TIMES_SURGICAL_OUT_DATETIME": {
"value": 3
},
"TMP_CLINICAL_TIMES_ROOM_IN_DATETIME": {
"value": 4
},
"TMP_CLINICAL_TIMES_ROOM_OUT_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_BLOCK_OUT_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_BLOCK_IN_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_SURGICAL_IN_DATETIME": {
"value": null
}
}
}'
select L1.[Value] from OPENJSON (@j, '$.coreTimes') AS L1
我在 SQL Server 2017 的 NVARCHAR(MAX)
类型的列中有这个 Json:
{
"coreTimes": {
"TMP_CLINICAL_TIMES_ANESTHESIOLOGY_OUT_DATETIME": {
"value": 1
},
"TMP_CLINICAL_TIMES_ANESTHESIOLOGY_IN_DATETIME": {
"value": 2
},
"TMP_CLINICAL_TIMES_SURGICAL_OUT_DATETIME": {
"value": 3
},
"TMP_CLINICAL_TIMES_ROOM_IN_DATETIME": {
"value": 4
},
"TMP_CLINICAL_TIMES_ROOM_OUT_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_BLOCK_OUT_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_BLOCK_IN_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_SURGICAL_IN_DATETIME": {
"value": null
}
}
}
我需要这个结果:
Column
{"value":1}
{"value":2}
{"value":3}
{"value":4}
{"value":null}
{"value":null}
{"value":null}
如果 json 属性可以动态更改(键 'coreTimes' 是固定的),我可以使用什么 SQL Server 2017 函数来获得此结果?
我在 Oracle 中使用过:
SELECT res.*
FROM sopinterventionsaux ,
JSON_TABLE ( operating_times, '$.coreTimes.*'
COLUMNS (
value VARCHAR2 ( 2000 ) FORMAT JSON PATH '$'
)
) res
在 Postgre 中SQL:
select value from table, jsonb_each(column-> 'coreTimes')
然后在 SQL 服务器中?
如果我理解正确,您需要使用 OPENJSON() 和默认架构来解析输入 JSON
。在这种情况下 OPENJSON()
returns 具有列 key
、value
和 type
:
Table:
DECLARE @json nvarchar(max) = N'{
"coreTimes": {
"TMP_CLINICAL_TIMES_ANESTHESIOLOGY_OUT_DATETIME": {
"value": 1
},
"TMP_CLINICAL_TIMES_ANESTHESIOLOGY_IN_DATETIME": {
"value": 2
},
"TMP_CLINICAL_TIMES_SURGICAL_OUT_DATETIME": {
"value": 3
},
"TMP_CLINICAL_TIMES_ROOM_IN_DATETIME": {
"value": 4
},
"TMP_CLINICAL_TIMES_ROOM_OUT_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_BLOCK_OUT_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_BLOCK_IN_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_SURGICAL_IN_DATETIME": {
"value": null
}
}
}'
CREATE TABLE Data (JsonData nvarchar(max))
INSERT INTO Data (JsonData) VALUES (@json)
声明:
SELECT j.[value]
FROM Data d
CROSS APPLY OPENJSON(d.JsonData, '$.coreTimes') j
结果:
---------------
value
---------------
{"value": 1}
{"value": 2}
{"value": 3}
{"value": 4}
{"value": null}
{"value": null}
{"value": null}
{"value": null}
如果您想获得 value
键的准确值,您需要将 OPENJSON()
与显式模式(带有列定义)和附加的 APPLY
运算符一起使用。
声明:
SELECT j2.[value]
FROM Data d
CROSS APPLY OPENJSON(d.JsonData, '$.coreTimes') j1
CROSS APPLY OPENJSON(j1.[value]) WITH ([value] int '$.value') j2
结果:
-----
value
-----
1
2
3
4
NULL
NULL
NULL
NULL
请尝试以下操作:
declare @j nvarchar(max) = '{
"coreTimes": {
"TMP_CLINICAL_TIMES_ANESTHESIOLOGY_OUT_DATETIME": {
"value": 1
},
"TMP_CLINICAL_TIMES_ANESTHESIOLOGY_IN_DATETIME": {
"value": 2
},
"TMP_CLINICAL_TIMES_SURGICAL_OUT_DATETIME": {
"value": 3
},
"TMP_CLINICAL_TIMES_ROOM_IN_DATETIME": {
"value": 4
},
"TMP_CLINICAL_TIMES_ROOM_OUT_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_BLOCK_OUT_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_BLOCK_IN_DATETIME": {
"value": null
},
"TMP_CLINICAL_TIMES_SURGICAL_IN_DATETIME": {
"value": null
}
}
}'
select L1.[Value] from OPENJSON (@j, '$.coreTimes') AS L1