Domanda

Per un database con 3 tabelle:

  1. Azienda
  2. Dipartimento
  3. Employeeeexpartment

Vorrei un XML come sotto:

<Companies>
  <Company>
    <CompanyName></CompanyName>
    <CompanyId></CompanyId>
    <..></..>
    <Departments>
       <Name></Name>
       <..></..>
       <Employees>
          <Employee>
             <FirstName></FirstName>
             <LastName></LastName>
             .. .. ..
          </Employee>
          .. .. ..
       </Employees>
    </Departments>
    .. .. ..
  </Company>
  .. .. .. 
</Companies>

Laddove l'azienda ripete (tag), i dipartimenti all'interno dell'azienda ripetono (tag) e i dipartimenti dei dipendenti si ripetono (tag) ripetendo, intendo dire che ci sono più di un numero di questi elementi e non i dati.

Relazioni

  1. Company e Department sono correlati attraverso FK in Department tabella che si collega a CompanyId in Company tavolo.

  2. Department e EmployeeInDepartment sono correlati tramite "fk" in EmployeeInDepartment tabella che si collega a DepartmentId in Department tavolo.

Domanda:

Esegui il database di seguito sul database Pub e controlla XML, avrà più di un lavoro con ID 10

select jobs.job_id 'JobId',
job_desc 'Desc',
(
    select emp_id 'EmployeeId',fname 'FirstName',lname 'LastName' from employee where job_id = jobs.job_id for xml path('Emploees'),type
)
from jobs
inner join 
employee on  jobs.job_id = employee.job_id
for xml path('employees')

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top