Control on XML elements nesting using FOR XML

有些话、适合烂在心里 提交于 2019-12-02 20:31:44

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/41877161/control-on-xml-elements-nesting-using-for-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!