我有一些Json存储在SQL Server 2016表格中(partitial)
{
"AFP": [
{
"AGREEMENTID": "29040400001330",
"LoanAccounts": {
"Product": "OD003",
"BUCKET": 0,
"ZONE": "MUMBAI ZONE",
"Region": "MUMBAI METRO-CENTRAL REGION",
"STATE": "GOA",
"Year": 2017,
"Month": 10,
"Day": 13
},
"FeedbackInfo": {
"FeedbackDate": "2017-10-13T12:07:44.2317198",
"DispositionDate": "2017-10-13T12:07:44.2317198",
"DispositionCode": "PR"
},
"PaymentInfo": {
"ReceiptNo": "2000000170",
"ReceiptDate": "2017-10-13T12:07:42.1218299",
"PaymentMode": "Cheque",
"Amount": 200,
"PaymentStatus": "CollectionBatchCreated"
}
}
]
}
下的表模式
create table tblHistoricalDataDemo(
AGREEMENTID nvarchar(40)
,Year_Json nvarchar(4000)
)
我想将记录从JSON提取为关系格式
AgreementID产品存档.... PaymentStatus
我尝试过以下但我做错了,我无法得到结果
SELECT AGREEMENTID,
JSON_VALUE(Year_Json, '$.LoanAccounts') AS records
FROM tblHistoricalDataDemo
答案 0 :(得分:2)
使用OPENJSON
内置表值函数:
SELECT *
FROM tblHistoricalDataDemo
CROSS APPLY
OPENJSON(Year_Json, '$.AFP') WITH
(
-- You don't have to specify the json path
-- if the column name is the same as the json name
AGREEMENTID bigint
)
As afp
CROSS APPLY
OPENJSON(Year_Json, '$.AFP') WITH
(
Product varchar(10) '$.LoanAccounts.Product',
bucket int '$.LoanAccounts.BUCKET'
)
As LoanAccounts
答案 1 :(得分:0)
如果JSON中的数组具有固定数量的元素,请使用
router.get("/", (req, res) => {
User.findById(req.user._id, (err, user) => {
if (err){
console.log(err);
}
Item.find({}, (err, allItems) => {
if (err) {
console.log(err);
}
res.render("products", { items: allItems });
});
});
});
如果AFP只有1个元素,
$.P1[x]
在雅各布的SQLFiddle中运行。