OPENJSON to Parse Field getting Subquery returned more than 1 value error

OPENJSON to Parse Field getting Subquery returned more than 1 value error

我在数据库中有一个对象,我相信它可以使用 OPENJSON 进行解析。如果有更好的解决方案,我绝对愿意接受。以下值是虚构的。

TABLE

 DROP TABLE IF EXISTS #temp;
CREATE TABLE #temp (
ID INT,
Object VARCHAR(MAX)
);

INSERT INTO #temp
(
    ID,
    Object
)
VALUES
(   1, '{   "Country": "US",   "CountryName": "United States of America",   "Inflation": 5.0 }'),
(   2, '{   "Country": "MX",   "CountryName": "Mexico",   "Inflation": 6.0 }'),
(   3, '{   "Country": "CA",   "CountryName": "Canada,   "Inflation": 5.5 }');

尝试的解决方案

SELECT *
  FROM OPENJSON((SELECT Object FROM #temp))
  WITH (
         Country CHAR(2) '$.Country',
         CountryName VARCHAR(MAX) '$.CountryName',
         Inflation DECIMAL(2,1) '$.Inflation'
       )     

结果

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

期望的结果

ID Country Country Name Inflation
1 US United States of America 5.0
2 MX Mexico 6.0
1 CA Canada 5.5

如有任何帮助,我们将不胜感激!

在你加了"之后加...使用CROSS APPLY

Select A.ID 
      ,B.*
 From  #temp A
 Cross Apply (
            SELECT *
              FROM OPENJSON(Object)
              WITH (
                     Country CHAR(2) '$.Country',
                     CountryName VARCHAR(MAX) '$.CountryName',
                     Inflation DECIMAL(2,1) '$.Inflation'
                   )     
             ) B

结果

编辑

- 如果您的 JSON 就这么简单,以下是提高性能的方法。

 Select A.ID 
      ,Country     = JSON_VALUE(Object,'$.Country')
      ,CountryName = JSON_VALUE(Object,'$.CountryName')
      ,Inflation   = JSON_VALUE(Object,'$.Inflation')
 From  #Temp A