2017-10-25 3 views
1

Ich versuche, zwei SQL-Tabellen mit innerer Verknüpfung beizutreten und sie dann als JSON von meiner Prozedur zurückzugeben.SQL Inner Join mit Für JSON-Hierarchie

Meine select-Anweisung ist:

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) 

und das Ergebnis ist wie folgt aus:

"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 
} 
] 
} 

Das Problem ist, dass ich zu verschachtelt die Informationen nicht in dem Array AddressDetails möge. Ist es möglich, dass die Information dort draußen ist, damit ich 2 flache Objekte ohne verschachtelte Informationen empfangen kann?

Dank

Antwort

0

Betrachten Sie die PATH-Modus mit Punktsyntax und Karte alle Felder Adresse wie besprochen in docs verwenden.

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) 

Alternativ können Sie eine abgeleitete Tabelle:

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) 
+0

Glad zu helfen. Neugierig, OP, welche Lösung hat für Sie funktioniert? – Parfait