我尝试使用SQL提取以下JSON消息:
{
"document": {
"Invoice": {
"_NavRecordId": "Purch. Inv. Header: 06IF+230033",
"InvoiceNumber": "06IF+230033",
"PurchaseOrderNumber": "P0032259",
"InvoiceLine": [
{
"_NavRecordId": "Purch. Inv. Line: 06IF+230033,1",
"item": "A05284",
"Quantity": "2",
"PurchaseOrderLineNumber": "1"
},
{
"_NavRecordId": "Purch. Inv. Line: 06IF+230033,2",
"item": "A00019",
"Quantity": "10",
"PurchaseOrderLineNumber": "2"
}
]
}
}
}
不幸的是,我无法将这些数据放到一个表中,并将每一条数据线作为它自己的记录。
我已经试过下面的代码:
DECLARE @json NVARCHAR(MAX) = '{
"document": {
"Invoice": {
"_NavRecordId": "Purch. Inv. Header: 06IF+230033",
"InvoiceNumber": "06IF+230033",
"PurchaseOrderNumber": "P0032259",
"InvoiceLine": [
{
"_NavRecordId": "Purch. Inv. Line: 06IF+230033,1",
"item": "A05284",
"Quantity": "2",
"PurchaseOrderLineNumber": "1"
},
{
"_NavRecordId": "Purch. Inv. Line: 06IF+230033,2",
"item": "A00019",
"Quantity": "10",
"PurchaseOrderLineNumber": "2"
}
]
}
}
}';
SELECT
JSON_VALUE(@json, '$.document.Invoice._NavRecordId') AS NavRecordId,
JSON_VALUE(@json, '$.document.Invoice.InvoiceNumber') AS InvoiceNumber,
JSON_VALUE(@json, '$.document.Invoice.PurchaseOrderNumber') AS PurchaseOrderNumber,
JSON_VALUE(@json, '$.document.Invoice.InvoiceLine.item') AS Item,
JSON_VALUE(@json, '$.document.Invoice.InvoiceLine.Quantity') AS Quantity,
JSON_VALUE(@json, '$.document.Invoice.InvoiceLine.PurchaseOrderLineNumber') AS PurchaseOrderLineNumber
FROM
OPENJSON(@json, '$.document.Invoice.InvoiceLine')
WITH (
_NavRecordId nvarchar(100) '$._NavRecordId',
item nvarchar(100),
Quantity int,
PurchaseOrderLineNumber int
) AS il;
不幸的是,我无法填写项目,数量和采购订单行号与尊重的数据
2条答案
按热度按时间xeufq47z1#
你需要这样的东西:
Demo here
rnmwe5a22#
试试这个- SQL Server只需将JSON放入变量JSON 1。DEVICE@json1 NVarChar(2048)
结果