0

我有几个未连接的表,但希望使用 SQL Server 使用 FOR JSON 将数据作为 JSON 从查询中返回,可能使用子查询?

创建表格;

DECLARE @Filter AS TABLE
(
    filter_id int primary key,
    filter_field varchar(255),
    filter_values varchar(255)
)
DECLARE @Other AS TABLE
(
    other_id int primary key,
    other_field varchar(255),
    other_values varchar(255)
)
DECLARE @Data AS TABLE
(
    data_id int primary key,
    Col1 varchar(255),
    Col2 varchar(255),
    Col3 varchar(255)
)

插入数据;

INSERT INTO @Filter (filter_id, filter_field,filter_values) VALUES
(1, 'SC.Type','Parent'),
(2, 'ScanDateTime','20200620')

INSERT INTO @Other (other_id, other_field,other_values) VALUES
(1, 'header','This is the header'),
(2, 'footer','This is the footer')

INSERT INTO @Data (data_id,Col1,Col2,Col3) VALUES
(1, 'Val1','Val2','Val3'),
(2, 'Val4','Val5','Val6'),
(3, 'Val7','Val8','Val9')

我一直在@Data 的每一行中重复使用@Filter 和@Other。

获取以下结果的 JSON 查询将是什么;

{
    "filter":
        [
            {
                "field":"SC.Type",
                "values":"Parent"
            },
            {
                "field":"ScanDateTime"
                "values":"20200620"
            }
        ],
    "header":"This is the header",
    "footer":"This is the footer",
    "data":
        [
            {
                "col1":"Val1",
                "col2":"Val2",
                "col3":"Val3"
            },
            {
                "col1":"Val4",
                "col2":"Val5",
                "col3":"Val6"
            },
            {
                "col1":"Val7",
                "col2":"Val8",
                "col3":"Val9"
            }
        ]
}

任何帮助,将不胜感激。

4

1 回答 1

0

原答案:

一种可能的方法是以下语句:

SELECT 
   filter = (
      SELECT filter_field AS [field], filter_values AS [values]
      FROM @filter
      FOR JSON AUTO     
   ),
   MAX(CASE WHEN other_field = 'header' THEN other_values END) AS header,
   MAX(CASE WHEN other_field = 'footer' THEN other_values END) AS footer,
   data = (
      SELECT Col1, Col2, Col3
      FROM @Data
      FOR JSON AUTO
   )
FROM @Other   
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER

结果:

{
   "filter":[
       {"field":"SC.Type","values":"Parent"}, 
       {"field":"ScanDateTime","values":"20200620"}
   ],
   "header":"This is the header",
   "footer":"This is the footer",
   "data":[
       {"Col1":"Val1","Col2":"Val2","Col3":"Val3"}, 
       {"Col1":"Val4","Col2":"Val5","Col3":"Val6"}, 
       {"Col1":"Val7","Col2":"Val8","Col3":"Val9"}
   ]
}

更新:

如果@Other表有多个headerfooter行并且这些行的值不同(有效的 JSON 内容或文本),您需要使用字符串聚合的混合方法STRING_SPLIT()and JSON_MODIFY()

表:

DECLARE @Filter AS TABLE
(
   filter_id int primary key,
   filter_field varchar(255),
   filter_values varchar(255)
)
DECLARE @Other AS TABLE
(
   other_id int primary key,
   other_field varchar(255),
   other_values varchar(255)
)
DECLARE @Data AS TABLE
(
   data_id int primary key,
   Col1 varchar(255),
   Col2 varchar(255),
   Col3 varchar(255)
)
INSERT INTO @Filter (filter_id, filter_field, filter_values) VALUES
   (1, 'SC.Type','Parent'),
   (2, 'ScanDateTime','20200620')
INSERT INTO @Other (other_id, other_field, other_values) VALUES
   (1, 'header','[{"Row1":"Val1"},{"Row2":"Val2"}]'),
   (2, 'footer','This is the footer'),
   (3, 'other','Something different')
INSERT INTO @Data (data_id, Col1, Col2, Col3) VALUES
   (1, 'Val1','Val2','Val3'),
   (2, 'Val4','Val5','Val6'),
   (3, 'Val7','Val8','Val9')

陈述:

SELECT
   JSON_MODIFY(
      JSON_MODIFY(
         CONCAT(
            '{', 
            STRING_AGG(CONCAT(
               '"', 
               other_field , 
               '":', 
               CASE WHEN ISJSON(other_values) = 1 THEN other_values ELSE CONCAT('"', other_values, '"') END, 
               ''
            ), ','), 
            '}'
         ),
         '$.filter',
         JSON_QUERY((
            SELECT filter_field AS [field], filter_values AS [values]
            FROM @filter
            FOR JSON AUTO     
         ))
      ),
      '$.data',
      JSON_QUERY((
          SELECT Col1, Col2, Col3
          FROM @Data
          FOR JSON AUTO     
      ))   
   )   
FROM @Other

结果:

{
  "header":[
    {
      "Row1":"Val1"
    },
    {
      "Row2":"Val2"
    }
  ],
  "footer":"This is the footer",
  "other":"Something different",
  "filter":[
    {
      "field":"SC.Type",
      "values":"Parent"
    },
    {
      "field":"ScanDateTime",
      "values":"20200620"
    }
  ],
  "data":[
    {
      "Col1":"Val1",
      "Col2":"Val2",
      "Col3":"Val3"
    },
    {
      "Col1":"Val4",
      "Col2":"Val5",
      "Col3":"Val6"
    },
    {
      "Col1":"Val7",
      "Col2":"Val8",
      "Col3":"Val9"
    }
  ]
}
于 2020-06-19T21:22:35.557 回答