SQL内部联接与For JSON层次结构

时间:2017-10-25 15:45:48

标签: sql sql-server tsql stored-procedures

我尝试使用内部联接加入两个SQL表,然后从我的过程中将它们作为JSON返回。

我的选择陈述是:

SELECT 
    @CustomerAddressesJSON =
        (SELECT              
             Address.AddressID, Address.CustomerID,
             Address.AddressTypeID, Address.IsPrimary,
             CountryID, StateID, CountyID, DistrictID,
             StreetID, StreetNumber, PostalCode,
             AdditionalInformation, AddressImageID,
             CreatedOn, CreatedBy
         FROM 
             [sCustomerManagement].[tCustomerAddresses] Address
         INNER JOIN 
             [sCustomerManagement].[tAddresses] AddressDetails ON Address.AddressID = AddressDetails.AddressID
         WHERE 
             CustomerID = @CustomerID
         FOR JSON AUTO)

结果是这样的:

"customerAddressesJSON": "[
{
"AddressID":1,
"CustomerID":1,
"AddressTypeID":"T",
"IsPrimary":true,
"AddressDetails":[
{
"CountryID":1,"StateID":1,"CountyID":1,"DistrictID":1,"StreetID":1,"StreetNumber":"125","PostalCode":"1000","AdditionalInformation":"Metro Sofia","CreatedOn":"2017-10-24T11:46:20.1933333","CreatedBy":24
}
]
},
{
"AddressID":2,
"CustomerID":1,
"AddressTypeID":"T",
"IsPrimary":true,
"AddressDetails":[
{
"CountryID":1,"StateID":1,"CountyID":1,"DistrictID":1,"StreetID":1,"StreetNumber":"125","PostalCode":"1000","AdditionalInformation":"Metro Sofia","CreatedOn":"2017-10-24T11:46:20.1933333","CreatedBy":24
}
]
}

问题在于我不希望嵌套数组AddressDetails中的信息。那里的信息是否可能在外面,所以我可以收到2个扁平物体,没有嵌套信息?

由于

1 个答案:

答案 0 :(得分:1)

考虑使用带有点语法的PATH模式,并将所有字段映射到地址,如docs中所述。

SELECT 
    @CustomerAddressesJSON =
        (SELECT              
             a.AddressID AS 'Address.AddressID', a.CustomerID AS 'Address.CustomerID',
             a.AddressTypeID AS 'Address.AddressTypeID', a,IsPrimary AS 'Address.IsPrimary',
             d.CountryID AS 'Address.CountryID', d.StateID AS 'Address.StateID',
             d.CountyID AS 'Address.CountyID', d.DistrictID AS 'Address.DistrictID',
             d.StreetID As 'Address.StreetID', d.StreetNumber AS 'Address.StreetNumber', 
             d.PostalCode AS 'Address.PostalCode',
             d.AdditionalInformation AS 'Address.AdditionalInformation', 
             d.AddressImageID AS 'Address.AddressImageID',
             d.CreatedOn AS 'Address.CreatedOn', d.CreatedBy AS 'Address.CreatedBy'
         FROM 
             [sCustomerManagement].[tCustomerAddresses] a
         INNER JOIN 
             [sCustomerManagement].[tAddresses] d ON a.AddressID = d.AddressID
         WHERE 
             a.CustomerID = @CustomerID
         FOR JSON AUTO)

或者,使用派生表:

SELECT 
    @CustomerAddressesJSON =
        (SELECT m.* 
         FROM 
             (SELECT a.AddressID, a.CustomerID, a.AddressTypeID, a,IsPrimary,
                     d.CountryID, d.StateID, d.CountyID, d.DistrictID,
                     d.StreetID, d.StreetNumber, d.PostalCode,
                     d.AdditionalInformation, d.AddressImageID,
                     d.CreatedOn, d.CreatedBy
              FROM 
                   [sCustomerManagement].[tCustomerAddresses] a
              INNER JOIN 
                   [sCustomerManagement].[tAddresses] d ON a.AddressID = d.AddressID
              WHERE 
                    a.CustomerID = @CustomerID
             ) AS m
         FOR JSON AUTO)