使用 FOR XML 控制 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>

正如我所说,我发现每个 xml 元素加入一次视图(这里是表变量)是一种解决方案:

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

这会产生所需的输出.

这种使用 for xml auto 进行多重连接的技术是否有效?

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

推荐答案

在别名中使用@,在xml中生成attributes.更简单的方法来做到这一点

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') 

结果:

<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>

相关文章