2017-03-01 48 views
0

我需要以特定的格式XML返回成單獨的節點元素

<Applicants xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <Applicant> 
     <Name>John Smith</Name> 
     <Address>1 Smiths Close</Address> 
    </Applicant> 
    <Applicant> 
     <Name>Peter Smith</Name> 
     <Address>2 Smiths Close</Address> 
    </Applicant> 
</Applicants> 

返回的結果,但我目前的查詢

select A.Name 
    , ANL.name 
    , A.address AS Address 
    , ANL.address AS Address 
    from Agents A 
     left join AgentsNonLatin ANL 
      On A.Pn = ANL.Pn AND A.Kd = ANL.kd 
    FOR XML RAW('Applicant'), ROOT('Applicants'), ELEMENTS XSINIL 

我讓他們合併成一個單一<Applicant>節點。

<Applicants xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <Applicant> 
    <Name>John Smith</Name> 
    <Name>Peter Smith</Name> 
    <Address>1 Smiths Close</Address> 
    <Address>2 Smiths Close</Address> 
    </Applicant> 
</Applicants> 

有關需要更改哪些內容以便以所需格式返回的任何想法?

回答

2

使用union這裏

SELECT 
     A.Name 
    , A.Address 
    FROM Agents A 
UNION 
SELECT 
     ANL.Name 
    , ANL.Address AS Address 
    FROM AgentsNonLatin ANL 
FOR XML RAW('Applicant'), ROOT('Applicants'), ELEMENTS XSINIL