我有以下名为( C:\ temp \ test.json )的JSON文件,它有一条主记录和三条子记录,结构如下:
[{
"dc:type":{
"dc:title":"AMC",
"dc:type":"recordType",
"rdf:resource":"https://tmkl.at1/16777434"
},
"dc:title":"CCP00113620",
"RM_Approved_on":"2011-12-19T13:04:36Z",
"Title":"RAM PROGRAM PLAN",
"Project":{
"oslc_cm:label":"HTLS",
"rdf:resource":"https://tmkl.at1/16789988-34271859"
},
"Drawing":"N",
"Attachments":{
"oslc_cm:results":[
{
"rdf:about":"https://tmkl.at1/16777434-33668052/field/Attachments/attachment/16782994-34738717",
"dc:type":{
"dc:title":"AMC",
"dc:type":"recordType",
"rdf:resource":"https://tmkl.at1./16777434"
},
"dc:title":"Doc-review-sheet-CCP00113620.xls",
"filename":"Doc-1234-CCP00113620.xls"
},
{
"rdf:about":"https://tmkl.at1/16777434-33668052/field/Attachments/attachment/16782994-34736984",
"dc:type":{
"dc:title":"AMC",
"dc:type":"recordType",
"rdf:resource":"https://tmkl.at1./16777434"
},
"dc:title":"Doc-review-sheet-CCP00113620-JD.xls",
"filename":"Doc-CCP00113620-JD300.xls"
},
{
"rdf:about":"https://tmkl.at1/16777434-33668052/field/Attachments/attachment/16782994-34738722",
"dc:type":{
"dc:title":"AMC",
"dc:type":"recordType",
"rdf:resource":"https://tmkl.at1./16777434"
},
"dc:title":"3CU 05007 0009 UMZZA-Rev01-B_edited.doc",
"filename":"UMZZA-Rev01-B_edited.doc"
}
],
"oslc_cm:collref":"https://tmkl.at1/16777434-33668052/field/Attachments"
},
"rdf:about":"https://tmkl.at1/16777434-33668052"
},
{
"dc:type":{
"dc:title":"AMC",
"dc:type":"recordType",
"rdf:resource":"https://tmkl.at1./16777434"
}]
我想使用sql SQL Server 2016查询读取此文件( C:\ temp \ test.json )并获得以下结果:
id Project Title Filename Web-address
CCP00113620 HTLS RAM PROGRAM PLAN Doc-1234-CCP00113620.xls https://tmkl.at1/16777434-33668052/field/Attachments/attachment/16782994-34738717
CCP00113620 HTLS RAM PROGRAM PLAN Doc-CCP00113620-JD300.xls https://tmkl.at1/16777434-33668052/field/Attachments/attachment/16782994-34736984
CCP00113620 HTLS RAM PROGRAM PLAN UMZZA-Rev01-B_edited.doc https://tmkl.at1/16777434-33668052/field/Attachments/attachment/16782994-34738722
我尝试了以下查询:
SELECT ecn_att.*,q1.*
FROM OPENROWSET (BULK 'd:\temp\ecn_att.json', SINGLE_CLOB) as j
CROSS APPLY OPENJSON(BulkColumn)
WITH(
[id] [varchar](16),
[Project] [varchar](200),
[Title] [varchar](200),
[Attachments] nvarchar(max) AS JSON
) AS ecn_att
CROSS APPLY OPENJSON ([Attachments])
WITH (
[dc:type] [varchar](max)
) as Q1
我无法获取子记录
答案 0 :(得分:1)
这是返回预期结果的正确查询。棘手的部分是用双引号括起子元素[oslc_cm:results]的名称:
SELECT ecn_att.*,q1.* FROM OPENROWSET (BULK 'd:\temp\ecn_att.json', SINGLE_CLOB) as j
CROSS APPLY OPENJSON(BulkColumn)
WITH(
[id] [varchar](16),
[Project] [varchar](200),
[Title] [varchar](200),
[Attachments] nvarchar(max) AS JSON
) AS ecn_att
CROSS APPLY OPENJSON ([Attachments],'$."oslc_cm:results"') WITH ([filename] nvarchar(max), [rdf:about] nvarchar(max)) as Q1