UnDiUdin
UnDiUdin

Reputation: 15374

Control on XML elements nesting using FOR XML

I state my problem by example. In fact i foudna solution after trying in many ways but i would like to ask whether this solution is good or if for any reason it is better to use an alternative approach. In fact i need to control how elements are created.

i first made a view containing all the data i needed and then i selected from teh view by joining the view more times.

I reproduced the "complexity" here using a local variable instead of a view:

DECLARE @Employees table(  
    EmpID int NOT NULL,  
    Name nvarchar(50),  
    Surname nvarchar(50),  
    DateOfBirth date,
    DepartmentID int,
    AccessLevel int);
insert into  @Employees    values ('1', 'John','Doe','1980-01-31',100,5)
insert into  @Employees    values ('2', 'Mary','Rose','1971-02-27',102,3)
insert into  @Employees    values ('3', 'Luke','Perry','1995-12-01',104,1)

This is the desired result (employee, department and security are differeent elements - my problem was to create employee department and security just like in this example):

<employee Name="John" Surname="Doe" DateOfBirth="1980-01-31">
  <department DepartmentID="100">
    <security AccessLevel="5" />
  </department>
</employee>
<employee Name="Mary" Surname="Rose" DateOfBirth="1971-02-27">
  <department DepartmentID="102">
    <security AccessLevel="3" />
  </department>
</employee>
<employee Name="Luke" Surname="Perry" DateOfBirth="1995-12-01">
  <department DepartmentID="104">
    <security AccessLevel="1" />
  </department>
</employee>

As i said i found out that joining the view (here the table variable) one time per xml element is a solution:

-- declare @Employees table as above and then:
    select
      employee.Name,
      employee.Surname,
      employee.DateOfBirth,
      department.DepartmentID, 
      security.AccessLevel from @Employees employee
    join @Employees department on department.DepartmentID = employee.DepartmentID
    join @Employees security on security.AccessLevel = employee.AccessLevel
    for xml auto

this produces the desired output.

Is this techniwue of multiple joins with for xml auto valid or not?

Upvotes: 0

Views: 53

Answers (1)

Pரதீப்
Pரதீப்

Reputation: 93694

Use @ in alias names to generate attributes in xml. Much simpler way to do this

SELECT NAME         AS [@Name],
       Surname      AS [@Surname],
       DateOfBirth  AS [@DateOfBirth],
       DepartmentID AS [department/@DepartmentID],
       AccessLevel  AS [department/security/@AccessLevel]
FROM   @Employees
FOR xml path('employee') 

Result:

<employee Name="John" Surname="Doe" DateOfBirth="1980-01-31">
  <department DepartmentID="100">
    <security AccessLevel="5" />
  </department>
</employee>
<employee Name="Mary" Surname="Rose" DateOfBirth="1971-02-27">
  <department DepartmentID="102">
    <security AccessLevel="3" />
  </department>
</employee>
<employee Name="Luke" Surname="Perry" DateOfBirth="1995-12-01">
  <department DepartmentID="104">
    <security AccessLevel="1" />
  </department>
</employee>

Upvotes: 3

Related Questions