使用FOR XML PATH的XML顺序

Val*_*l K 1 xml t-sql sql-server

我写了一个XML查询,它以我想要的格式不同的格式创建一个订单:

select
    'sample' "@ponumber",
    'xxxxxx' "@cust",
    'yyyyyy' "@shipto",
    '999999'  "line/material",
    '20'      "line/qty",
    '777777'  "line/material",
    '20'      "line/qty"
for 
    xml path('root')
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

<root ponumber="sample" cust="xxxxxx" shipto="yyyyyy">
  <line>
    <material>999999</material>
    <qty>20</qty>
     <material>777777</material>
    <qty>20</qty>
  </line>
</root>
Run Code Online (Sandbox Code Playgroud)

所需的输出是这样的:

<root ponumber="sample" cust="xxxxxx" shipto="yyyyyy">
  <line>
    <material>999999</material>
    <qty>20</qty>
  </line>
  <line>
    <material>777777</material>
    <qty>20</qty>
  </line>
</root>
Run Code Online (Sandbox Code Playgroud)

在查询中需要修改什么?

谢谢!

TT.*_*TT. 5

这将为您提供所需的输出.它的工作方式进行了说明这里对堆栈溢出.

select
    'sample' "@ponumber",
    'xxxxxx' "@cust",
    'yyyyyy' "@shipto",
    '999999'  "line/material",
    '20'      "line/qty",
    '',
    '777777'  "line/material",
    '20'      "line/qty"
for 
    xml path('root')
Run Code Online (Sandbox Code Playgroud)

产生

<root ponumber="sample" cust="xxxxxx" shipto="yyyyyy">
  <line>
    <material>999999</material>
    <qty>20</qty>
  </line>
  <line>
    <material>777777</material>
    <qty>20</qty>
  </line>
</root>
Run Code Online (Sandbox Code Playgroud)