• <tfoot id='L3rVH'></tfoot>
      <bdo id='L3rVH'></bdo><ul id='L3rVH'></ul>

        <i id='L3rVH'><tr id='L3rVH'><dt id='L3rVH'><q id='L3rVH'><span id='L3rVH'><b id='L3rVH'><form id='L3rVH'><ins id='L3rVH'></ins><ul id='L3rVH'></ul><sub id='L3rVH'></sub></form><legend id='L3rVH'></legend><bdo id='L3rVH'><pre id='L3rVH'><center id='L3rVH'></center></pre></bdo></b><th id='L3rVH'></th></span></q></dt></tr></i><div id='L3rVH'><tfoot id='L3rVH'></tfoot><dl id='L3rVH'><fieldset id='L3rVH'></fieldset></dl></div>
      1. <small id='L3rVH'></small><noframes id='L3rVH'>

      2. <legend id='L3rVH'><style id='L3rVH'><dir id='L3rVH'><q id='L3rVH'></q></dir></style></legend>
      3. 用于 JSON 输出整数数组的 SQL Server 2016

        时间:2023-08-19
          <legend id='wNC0q'><style id='wNC0q'><dir id='wNC0q'><q id='wNC0q'></q></dir></style></legend>
          • <bdo id='wNC0q'></bdo><ul id='wNC0q'></ul>

              <small id='wNC0q'></small><noframes id='wNC0q'>

              <tfoot id='wNC0q'></tfoot>

                  <tbody id='wNC0q'></tbody>
                • <i id='wNC0q'><tr id='wNC0q'><dt id='wNC0q'><q id='wNC0q'><span id='wNC0q'><b id='wNC0q'><form id='wNC0q'><ins id='wNC0q'></ins><ul id='wNC0q'></ul><sub id='wNC0q'></sub></form><legend id='wNC0q'></legend><bdo id='wNC0q'><pre id='wNC0q'><center id='wNC0q'></center></pre></bdo></b><th id='wNC0q'></th></span></q></dt></tr></i><div id='wNC0q'><tfoot id='wNC0q'></tfoot><dl id='wNC0q'><fieldset id='wNC0q'></fieldset></dl></div>
                  本文介绍了用于 JSON 输出整数数组的 SQL Server 2016的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想使用 SQL Server 2016 的 For JSON 功能获取带有整数数组的 JSON.我被整数数组难住了.

                  I'd like to get JSON with an array of integers using SQL Server 2016's For JSON feature. I'm stumped on array of integers.

                  数据库表结构:

                  declare @Employees table (ID int, Name nvarchar(50))
                  insert into @Employees values
                  (1, 'Bob'),
                  (2, 'Randy')
                  
                  declare @Permissions table (ID int, PermissionName nvarchar(50))
                  insert into @Permissions values
                  (1, 'Post'),
                  (2, 'Comment'),
                  (3, 'Edit'),
                  (4, 'Delete')
                  
                  declare @EmployeePermissions table (EmployeeID int, PermissionID int)
                  insert into @EmployeePermissions values
                  (1, 1),
                  (1, 2),
                  (2, 1),
                  (2, 2),
                  (2, 3)
                  

                  预期结果:

                  {"EmployeePermissions": [
                    {"Employee":"Bob", "Permissions":[1,2]},
                    {"Employee":"Randy", "Permissions":[1,2,3]}
                  ]}
                  

                  这是我得到的最接近的,但不是我想要的.

                  This is the closest I've gotten, but not quite what I want.

                  select
                      e.Name as Employee,
                      (select 
                           convert(nvarchar(10),ep.PermissionID) as PermID 
                       from @EmployeePermissions ep 
                       where ep.EmployeeID=e.ID 
                       for json path) as 'Permissions'
                  from
                      @Employees e
                  for json path, root('EmployeePermissions')
                  

                  返回:

                  {"EmployeePermissions": [
                    {"Employee":"Bob", "Permissions":[{"permID":1},{"permID":2}]},
                    {"Employee":"Randy", "Permissions":[{"permID":1},{"permID":2},{"permID":3}]}
                  ]}
                  

                  推荐答案

                  在 AdventureWorks 2016 CTP3 JSON 示例中,您可以找到一个可以清理键值对数组并创建数组 od 值的函数:

                  In AdventureWorks 2016 CTP3 JSON sample you can find a function that can clean array of key:value pairs and create array od values:

                  DROP FUNCTION IF EXISTS dbo.ufnToRawJsonArray
                  GO
                  CREATE FUNCTION
                  [dbo].[ufnToRawJsonArray](@json nvarchar(max), @key nvarchar(400)) returns nvarchar(max)
                  AS BEGIN
                         declare @new nvarchar(max) = replace(@json, CONCAT('},{"', @key,'":'),',')
                         return '[' + substring(@new, 1 + (LEN(@key)+5), LEN(@new) -2 - (LEN(@key)+5)) + ']'
                  END
                  

                  只需提供 SELECT FOR JSON 表达式的结果作为 @json 参数和要删除的键的名称作为第二个参数.大概是这样的:

                  Just provide result of your SELECT FOR JSON expression as @json parameter and name of the key that you want to remove as second parameter. Probably something like:

                  select
                  e.Name as Employee,
                  JSON_QUERY(dbo.ufnToRawJsonArray(
                      (select 
                       convert(nvarchar(10),ep.PermissionID) as PermID 
                       from @EmployeePermissions ep 
                       where ep.EmployeeID=e.ID 
                       for json path)
                    , 'PermID'))
                     as 'Permissions'
                  from
                  @Employees e
                  for json path, root('EmployeePermissions')
                  

                  这篇关于用于 JSON 输出整数数组的 SQL Server 2016的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 PL/SQL 中创建或模拟二维数组 下一篇:SQL查询多个表,有多个连接和用逗号分隔列表的

                  相关文章

                  最新文章

                    <bdo id='ZuppC'></bdo><ul id='ZuppC'></ul>

                  <i id='ZuppC'><tr id='ZuppC'><dt id='ZuppC'><q id='ZuppC'><span id='ZuppC'><b id='ZuppC'><form id='ZuppC'><ins id='ZuppC'></ins><ul id='ZuppC'></ul><sub id='ZuppC'></sub></form><legend id='ZuppC'></legend><bdo id='ZuppC'><pre id='ZuppC'><center id='ZuppC'></center></pre></bdo></b><th id='ZuppC'></th></span></q></dt></tr></i><div id='ZuppC'><tfoot id='ZuppC'></tfoot><dl id='ZuppC'><fieldset id='ZuppC'></fieldset></dl></div>

                    1. <legend id='ZuppC'><style id='ZuppC'><dir id='ZuppC'><q id='ZuppC'></q></dir></style></legend>
                      <tfoot id='ZuppC'></tfoot>

                    2. <small id='ZuppC'></small><noframes id='ZuppC'>