如何从 Sql Server 2016 table 列中读取 JSON 文本
How to read JSON text from Sql Server 2016 table column
我在 Sql 服务器中有以下 JSon 数据。
{
"PartnerName": null,
"PartnerCurrencyCode": "UAD",
"PricingConditions": [
{
"PricingConditionId": 659853,
"ConditionTypeCode": "ABCD",
"ConcessionItemTypeCode": "ABC",
"PriceLevel": null
}
],
"CurrencyMultiplier": 0
}
预期结果是:
PartnerName PartnerCode PartnerCurrencyCode PricingConditionId
NULL NULL UAD 659853
我读到 Sql Server 2016 具有读取 JSON 数据的能力并尝试了以下查询。
查询 1:
SELECT * FROM OPENJSON(@json, '$')
查询 2:
SELECT *
FROM OPENJSON(@json)
WITH (PartnerName NVARCHAR(50) '$.PartnerName',
PartnerCode nvarchar(50) '$.PartnerCode',
PartnerCurrencyCode nvarchar(50) '$.PartnerCurrencyCode',
PricingConditionId nvarchar(50) '$.PricingConditions.PricingConditionId')
查询 2 结果:
PartnerName PartnerCode PartnerCurrencyCode PricingConditionId
NULL NULL UAD NULL
我期待 "PricingConditionId" 值为 - 659853
试试这个查询:
SELECT PartnerName,
PartnerCode,
PartnerCurrencyCode,
[PricingConditions[0]].PricingConditionId AS ProductID1
FROM OPENJSON (@json)
WITH (
PartnerName NVARCHAR(50),
PartnerCode NVARCHAR(50),
PartnerCurrencyCode NVARCHAR(50),
[PricingConditions[0]].PricingConditionId NVARCHAR(50)
) AS Partners
这里 link to a great article 讨论了解决您的问题的几种方法。
我在 Sql 服务器中有以下 JSon 数据。
{
"PartnerName": null,
"PartnerCurrencyCode": "UAD",
"PricingConditions": [
{
"PricingConditionId": 659853,
"ConditionTypeCode": "ABCD",
"ConcessionItemTypeCode": "ABC",
"PriceLevel": null
}
],
"CurrencyMultiplier": 0
}
预期结果是:
PartnerName PartnerCode PartnerCurrencyCode PricingConditionId
NULL NULL UAD 659853
我读到 Sql Server 2016 具有读取 JSON 数据的能力并尝试了以下查询。
查询 1:
SELECT * FROM OPENJSON(@json, '$')
查询 2:
SELECT *
FROM OPENJSON(@json)
WITH (PartnerName NVARCHAR(50) '$.PartnerName',
PartnerCode nvarchar(50) '$.PartnerCode',
PartnerCurrencyCode nvarchar(50) '$.PartnerCurrencyCode',
PricingConditionId nvarchar(50) '$.PricingConditions.PricingConditionId')
查询 2 结果:
PartnerName PartnerCode PartnerCurrencyCode PricingConditionId
NULL NULL UAD NULL
我期待 "PricingConditionId" 值为 - 659853
试试这个查询:
SELECT PartnerName,
PartnerCode,
PartnerCurrencyCode,
[PricingConditions[0]].PricingConditionId AS ProductID1
FROM OPENJSON (@json)
WITH (
PartnerName NVARCHAR(50),
PartnerCode NVARCHAR(50),
PartnerCurrencyCode NVARCHAR(50),
[PricingConditions[0]].PricingConditionId NVARCHAR(50)
) AS Partners
这里 link to a great article 讨论了解决您的问题的几种方法。