我想使用SQL Server 2016的For JSON功能获取带有整数数组的JSON .我对整数数组感到困惑.
数据库表结构:
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)
Run Code Online (Sandbox Code Playgroud)
期望的结果:
{"EmployeePermissions": [
{"Employee":"Bob", "Permissions":[1,2]},
{"Employee":"Randy", "Permissions":[1,2,3]}
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的最接近的,但不是我想要的.
select
e.Name as Employee,
(select
convert(nvarchar(10),ep.PermissionID) as PermID …Run Code Online (Sandbox Code Playgroud)