SQL Server 2016将数据值设置为json属性名称

时间:2017-08-14 06:38:00

标签: json sql-server-2016

这是我的表[类型]

TypeTable

这是我的表[Value]

ValueTable

我的sql脚本是

SELECT T.TypeId, V.ValueId
  FROM [dbo].[Type] AS T
INNER JOIN [dbo].[Value] AS V ON V.[TypeId] = T.[TypeId]
FOR JSON PATH

它会得到结果

[
   {
      "TypeId":"TypeId_1",
      "ValueId":"ValueId_1"
   },
   {
      "TypeId":"TypeId_1",
      "ValueId":"ValueId_2"
   },
   {
      "TypeId":"TypeId_1",
      "ValueId":"ValueId_3"
   },
   {
      "TypeId":"TypeId_2",
      "ValueId":"ValueId_4"
   },
   {
      "TypeId":"TypeId_2",
      "ValueId":"ValueId_5"
   }
]

但是我想让TypeId作为json属性名,它的值是ValueId的数组,如:

{
       "TypeId_1": [
          "ValueId_1",
          "ValueId_2",
          "ValueId_3"
       ],
       "TypeId_2": [
          "ValueId_4",
          "ValueId_5"
       ]
}

如何调整我的sql脚本?

0 个答案:

没有答案