如何将SQL查询作为数组追加到JSON的现有SQL查询中

时间:2019-08-02 00:59:18

标签: sql json tsql

我想从T-SQL输出以下JSON:

[{
   "foo" : 1,
   "bar" : "abc",
   "gar" : "2000-01-01"
   ...
   "arr" : ["a","b","c"]
 },
 {
   "foo" : 2,
   "bar" : "def",
   "gar" : "2000-01-02"
   ...
   "arr" : ["a","b","c"]
 }]

foo,bar和gar都来自同一表,但是arr是来自不同表和列的每个不同的行。如何构造我的SQL查询以JSON输出。

当前,我有:

Create or alter function func() returns table return
Select (
    select d.[Id], d.[Name], d.[eID], 
    case 
        when d.[Active] = 0 then 'False' 
        when d.[Active] = 1 then 'True' 
    end as Active, 
    d.[Description], d.[Created], d.[CreatedBy], e.Name as eName
    from table1 d
    inner join table2 e on e.ID = d.eID
    order by Active desc, Id asc
    for json path
) as dealtags
Go

这是对数组的查询:

select distinct name from table2

但是将查询输出到“ for json auto”会导致对象数组而不只是数组。

是否可以明智地做到这一点?

0 个答案:

没有答案