您了解 SQL Server 2016 中新的 JSON_ 支持,因此
假设我连续有这些数据
{
"BaseBoarding": 1,
"PriceLineStrategy": "PerPersonPerNight",
"Currency": "EUR",
"BasePriceLineList": [
{
"RoomTypeId": 1,
"PeriodId": 1,
"Price": 10.0
},
{
"RoomTypeId": 1,
"PeriodId": 2,
"Price": 100.0
},
{
"RoomTypeId": 1,
"PeriodId": 3,
"Price": 190.0
},
{
"RoomTypeId": 2,
"PeriodId": 1,
"Price": 280.0
},
{
"RoomTypeId": 2,
"PeriodId": 2,
"Price": 310.0
},
{
"RoomTypeId": 2,
"PeriodId": 3,
"Price": 340.0
}
]
}
如何以最高效的方式获取“BasePriceLineList”的项目数,最好使用内置的 JSON 支持?
需要写这样的东西:
SELECT JSON_ARRLEN(JsonDataCol, '$.BasePriceline') FROM MyTable
WHERE Id = 1
并得到 6 作为结果。
最佳答案
使用表而不是变量:
SELECT count(priceLineLists.RoomTypeId)
FROM Mytable
CROSS APPLY OPENJSON (JsonDataCol, N'$.BasePriceLineList')
WITH (
RoomTypeId int)
AS priceLineLists
关于json - 获取 SQL Server 2016 中 json 数组的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43141892/