FOR XML PATH。使用嵌入式select语句嵌套

时间:2015-08-25 16:26:42

标签: sql xml sql-server-2008 for-xml-path

从单个临时表中使用以下SQL时。

Select  DataType as '@Datatype',
 (Select
 'Order' as [Property/@name],
 Order1 as Property,
 null,
  'ExtCode' as [Property/@name],
 ExtCode as Property,
 null
 from #TempLine
 for xml path('Properties'), type
 )
 from #TempLine
 Group by DataType
 for xml path('Object'), root('Objects')

我收到以下XML

 <Objects>
      <Object Datatype="ItemInfo">
        <Properties>
          <Property name="Order">150825CREC004</Property>
          <Property name="ExtCode">150825CREC004#ISSMS</Property>
        </Properties>
        <Properties>
          <Property name="Order">150825CREC004</Property>
          <Property name="ExtCode">150825CREC004#IMF</Property>
        </Properties>
        <Properties>
          <Property name="Order">150825CREC004</Property>
          <Property name="ExtCode">150825CREC004#ILZF</Property>
        </Properties>
        <Properties>
          <Property name="Order">150825CREC004</Property>
          <Property name="ExtCode">150825CREC004#L5898.5W</Property>
        </Properties>
      </Object>
    </Objects>

我需要使用围绕属性的对象返回以下XML。

 <Objects>
      <Object Datatype="ItemInfo">
        <Properties>
          <Property name="Order">150825CREC004</Property>
          <Property name="ExtCode">150825CREC004#ISSMS</Property>
        </Properties>
      </Object>
      <Object Datatype="ItemInfo">
        <Properties>
          <Property name="Order">150825CREC004</Property>
          <Property name="ExtCode">150825CREC004#IMF</Property>
        </Properties>
      </Object>
      <Object Datatype="ItemInfo">
        <Properties>
          <Property name="Order">150825CREC004</Property>
          <Property name="ExtCode">150825CREC004#ILZF</Property>
        </Properties>
      </Object>
      <Object Datatype="ItemInfo">
        <Properties>
          <Property name="Order">150825CREC004</Property>
          <Property name="ExtCode">150825CREC004#L5898.5W</Property>
        </Properties>
      </Object>
    </Objects>

我认为我可以用嵌入式选择来做到这一点,但它没有成功。我需要能够让对象围绕每个属性而不是整个块。

0 个答案:

没有答案