如何从 table 列循环对象数组?

How to loop array of objects from table column?

Table 列:

id
---------
details

我的 table 列详细信息有 json 个对象

"data" : [ {
   "name" : "luke",
   "dob" : "12-10-90",
   "addr" : "sample1",
 }, 
{
    "name" : "sam",
    "dob" : "12-10-88",
    "addr" : "sample2"
   }
]

我想编写一个查询,它将给我如下记录:

| id   | name|  dob      |addr       |
|:-----|:----|:--- ------|:----------|
| 1    | luke|  12-10-90 |  sample1  |
| 1    | sam |  12-10-88 |  sample2  |

我试过

select 
    ID,
    JSON_VALUE(DETAILS, '$.data[0].name') , 
    JSON_VALUE(DETAILS, '$.data[0].dob') , 
        JSON_VALUE(DETAILS, '$.data[0].addr')
from 
    users;

计数结果:

id  cnt  name       dob       addr   
--  ---  ---------  --------  -------
 1    5  luke       12-10-90  sample1
 1    5  sam        12-10-88  sample2
 2    5  awd        12-10-90  sample1
 2    5  awdawdm    12-10-88  sample2
 2    5  sevsevsev  12-10-88  sample2

预计

id  cnt  name       dob       addr   
--  ---  ---------  --------  -------
 1    2  luke       12-10-90  sample1
 1    2  sam        12-10-88  sample2
 2    3  awd        12-10-90  sample1
 2    3  awdawdm    12-10-88  sample2
 2    3  sevsevsev  12-10-88  sample2

您可以遍历每条记录并使用 OPENJSON 来实现。

DECLARE @Id int
,@details NVARCHAR(MAX)

DROP TABLE IF EXISTS #Result
CREATE TABLE #Result (Id INT, [Name] VARCHAR(100),dob VARCHAR(10),addr VARCHAR(100))

DECLARE json_cursor CURSOR FOR 
SELECT Id,details 
FROM #DetailsTable  -- Replace with your table

OPEN json_cursor  
FETCH NEXT FROM json_cursor INTO @Id,@details

WHILE @@FETCH_STATUS = 0  
BEGIN  
      
      BEGIN
      INSERT INTO #Result
        SELECT @Id AS Id,*
            FROM OPENJSON(@details,N'$.data')
            WITH (   
                  
                            [Name] VARCHAR(100) '$.name',
                            dob VARCHAR(10) '$.dob',
                            adrr VARCHAR(100) '$.addr'
               
              
             ) 
      END

      FETCH NEXT FROM json_cursor INTO @Id,@details 
END 

CLOSE json_cursor  
DEALLOCATE json_cursor 

SELECT * FROM #Result

确保您的 json 对象包含在 '{}' 中 如下图

{"data" : [ {
   "name" : "luke",
   "dob" : "12-10-90",
   "addr" : "sample1",
 }, 
{
    "name" : "sam",
    "dob" : "12-10-88",
    "addr" : "sample2"
   }
]}

要从 json 数组中获取所有对象以及其他列,您可以使用 OpenJson() 和交叉应用,如下所示:

 create table users (id int, details nvarchar(max));
 insert into users values (1,N'{
               "data":[
                       {
                        "name" : "luke",
                        "dob" : "12-10-90",
                        "addr" : "sample1"
                       },
                       {
                         "name" : "sam",
                         "dob" : "12-10-88",
                         "addr" : "sample2"
                       }
                      ]
              }');

查询:

 SELECT u.id,count(details.name)over() cnt, details.name, details.dob,details.addr,details.*
 FROM users u CROSS APPLY OPENJSON (u.details,N'$.data') 
           WITH (   
                   
                 [Name] VARCHAR(100) '$.name',
                 dob VARCHAR(10) '$.dob',
                 addr VARCHAR(100) '$.addr'
                
               
  ) AS details

输出:

id cnt name dob addr Name dob addr
1 2 luke 12-10-90 sample1 luke 12-10-90 sample1
1 2 sam 12-10-88 sample2 sam 12-10-88 sample2

db<>fiddle here

您的代码正在运行@DLV。请检查。数据格式有问题。

 create table users (id int, details nvarchar(max));
 insert into users values (1,N'{"data" : 
 [ {     "name" : "luke",     "dob" : "12-10-90",     "addr" : "sample1",   },  
 {      "name" : "sam",      "dob" : "12-10-88",      "addr" : "sample2"    }  
 ]}');




 select 
     ID,
     JSON_VALUE(DETAILS, '$.data[0].name') , 
     JSON_VALUE(DETAILS, '$.data[0].dob') , 
     JSON_VALUE(DETAILS, '$.data[0].addr')
 from 
     users;

输出:

ID (No column name) (No column name) (No column name)
1 luke 12-10-90 sample1

db<>fiddle here