Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

export data to xml while maintaining proper relationships

$
0
0

I am trying to export data from some tables that have relationships to each other into xml format.

I am able to export the data to xml but I noticed the relationships do not stay intact.

For example currently the following is happening when I run the query below:

SELECT Top 1
 [Case].*,
 Person.*,
 CaseStatus.*
FROM [Case]
LEFT JOIN Person ON [Case].PersonID = Person.PersonID
LEFT JOIN CaseStatus ON [Case].CaseId = CaseStatus.CaseId
FOR XML AUTO,TYPE, ELEMENTS ,ROOT('Cases')

This is the XML result:

<Cases>
  <Case>
    <CaseId>1</CaseId>
    <CaseNumber>10TDU00001  </CaseNumber>
    <Person>
      <PersonId>1</PersonId>
      <CaseId>1</CaseId>
      <CaseStatus>
        <CaseStatusId>1</CaseStatusId>
        <CaseId>1</CaseId>
      </CaseStatus>
    </Person>
  </Case>
</Cases>

I want it to be in this format. Is it possible?

<Cases>
  <Case>
    <CaseId>1</CaseId>
    <CaseNumber>10TDU00001  </CaseNumber>
    <Person>
      <PersonId>1</PersonId>
      <ComplaintId>1</ComplaintId>
    </Person>
    <CaseStatus>
      <CaseStatusId>1</CaseStatusId>
      <CaseId>1</CaseId>
    </CaseStatus>
  </Case>
</Cases>


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>