Question

Je souhaite recevoir ma réponse XML au format suivant ...

<PersonDetails>
    <Name>Ajay</Name>
    <Age>29</Age>
    <ContactDetails>
        <ContactDetail>
            <ContactType>Mobile</ContactType>
            <ContactValue>9565649556</ContactValue>
        </ContactDetail>
        <ContactDetail>
            <ContactType>Email</ContactType>
            <ContactValue>ajay@yahoo.com</ContactValue>
        </ContactDetail>
    </ContactDetails>
</PersonDetails>

J'ai deux déclarations de sélection différentes ici ...

SELECT name, age FROM Person_Details
WHERE id = 12

SELECT Contact_Type, Contact_Value FROM Person_Contact_Details
Where id = 12

Toutes les suggestions ici ... J'ai essayé avec des combinaisons de FOR XML EXPLICIT / PATH.

Était-ce utile?

La solution

CHEMIN :

SELECT name AS Name
            , age AS Age
            , (SELECT Contact_Type AS ContactType
                , Contact_Value AS ContactValue
                FROM Person_Contact_Details c
                WHERE c.id = p.id
                FOR XML PATH('ContactDetail'), TYPE) AS ContactDetails
        FROM Person_Details p
        WHERE id = 12
        FOR XML PATH('PersonDetails')
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top