0

如何使用 FOR JSON SQL Server 2016 (TABLE to JSON) 在 JSON 对象内创建 JSON 数组
这是我的查询:

SELECT   
m.MeetingId AS tblMeeting_MeetingId,  
m.Attended AS tblMeeting_Attended, 
m3.CompanyId AS tblMeetingAttendants_CompanyId, 
m3.MeetingAttendantsId AS tblMeetingAttendants_AttendantNameWithTitle, 
m4.UserId AS tblMeetingAttendees_UserId, 
m5.BrokerId AS tblMeetingBroker_BrokerId 
FROM Bv.tblMeeting m 
LEFT JOIN Bv.tblMeetingAttendants m3 ON m.MeetingId = m3.MeetingId  
LEFT JOIN Bv.tblMeetingAttendees m4 ON m.MeetingId = m4.MeetingId  
LEFT JOIN Bv.tblMeetingBroker m5 ON m.MeetingId = m5.MeetingId  
WHERE m.MeetingId = 739 
FOR JSON AUTO, INCLUDE_NULL_VALUES

在此处输入图像描述

上面的查询给了我这样的结果:

[
{
    "tblMeeting_MeetingId": 739,
    "tblMeeting_Attended": false,
    "tblMeeting_MeetingSubject": " Benchmark China Internet Analyst",
    "m3": [
        {
            "tblMeetingAttendants_CompanyId": 83,
            "tblMeetingAttendants_AttendantNameWithTitle": 499,
            "m4": [
                {
                    "tblMeetingAttendees_UserId": null,
                    "m5": [
                        {
                            "tblMeetingBroker_BrokerId": 275
                        }
                    ]
                }
            ]
        },
        {
            "tblMeetingAttendants_CompanyId": 83,
            "tblMeetingAttendants_AttendantNameWithTitle": 500,
            "m4": [
                {
                    "tblMeetingAttendees_UserId": null,
                    "m5": [
                        {
                            "tblMeetingBroker_BrokerId": 275
                        }
                    ]
                }
            ]
        },
        {
            "tblMeetingAttendants_CompanyId": 83,
            "tblMeetingAttendants_AttendantNameWithTitle": 501,
            "m4": [
                {
                    "tblMeetingAttendees_UserId": null,
                    "m5": [
                        {
                            "tblMeetingBroker_BrokerId": 275
                        }
                    ]
                }
            ]
        }
    ]
}
]

但我想要这样的结果

[
{
    "tblMeeting_MeetingId": 739,
    "tblMeeting_Attended": false,
    "tblMeeting_MeetingSubject": " Benchmark China Internet Analyst",
    "tblMeetingAttendants_AttendantNameWithTitle": [499,500,501],
    "tblMeetingAttendees_UserId": null,
    "tblMeetingBroker_BrokerId": 275
}
]

请尽快回复
提前谢谢。

4

1 回答 1

0

如果不使用字符串连接并编写自己的函数,这似乎是不可能的。没有神奇的 JSON_ARRAY_AGGREGATE() 函数。我自己一直在找一个。这是一个相关的问题:SQL Server 2016 for JSON output integer array

于 2018-06-01T01:47:24.337 回答