【发布时间】:2019-06-26 16:44:07
【问题描述】:
我已经编写了一个代码,可以将信息从 JSON 行读取到 SQL 表中:
declare @pJSON varchar(max) = '
{
"School": "MiddleSchool",
"Password": "SchoolPassword",
"Attributes": [
{
"Type": "Exam",
"Value": "1"
},
{
"Type": "Class",
"Value": "11b"
},
{
"Type": "Math",
"Value": [
{
"ExamDate": "2019-01-01",
"Points": 100,
"Grade": 10,
"Notes": "Good"
}
]
}
]
} '
select ExamDate, Points, Grade, Notes
from OPENJSON(@pJSON, N'$.Attributes[2].Value')
cross apply openjson ([Value])
with
(
ExamDate date,
Points int,
Grade int,
Notes varchar(max)
) as [value]
代码运行良好,但我真的很讨厌 N'$.Attributes[2].Value' 部分。考试信息可以在第一、第二、第三位,所以[2] 对我来说真的不起作用。你对我有什么建议,我该如何改进这段代码?谢谢!
【问题讨论】:
标签: sql json sql-server parsing sql-server-2016