Sql - FOR XML路径查询问题

时间:2016-10-28 13:32:35

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

我的查询如下(样本)。

Select 'Somthing' as Title,
       'Some notes' as Notes,
        (Select Path1
        From (Select 'One' Path1
              union
              Select 'Two' Path1
              union
              Select 'Three' Path1) T        
         FOR XML PATH('Image'),ROOT('Images'), ELEMENTS, TYPE),

         'Other value' as Value

FOR XML PATH('ItemRow'),TYPE,ELEMENTS  

xml以下的输出

<ItemRow>
  <Title>Somthing</Title>
  <Notes>Some notes</Notes>
  <Images>
    <Image>
      <Path1>One</Path1>
    </Image>
    <Image>
      <Path1>Two</Path1>
    </Image>
    <Image>
      <Path1>Three</Path1>
    </Image>
  </Images>
  <Value>Other value</Value>
</ItemRow>

我正在尝试将Notes和Images放入父节点,因此它应该如下所示

<ItemRow>
  <Title>Somthing</Title>
  <SomeParentNode>
    <Notes>Some notes</Notes>
    <Images>
      <Image>
        <Path1>One</Path1>
      </Image>
      <Image>
        <Path1>Two</Path1>
      </Image>
      <Image>
        <Path1>Three</Path1>
      </Image>
    </Images>
  </SomeParentNode>
  <Value>Other value</Value>
</ItemRow>

这可能吗?

2 个答案:

答案 0 :(得分:2)

只需像这样添加SomeParentNode

Select 'Somthing' as Title,
       'Some notes' as 'SomeParentNode/Notes', -- here
        (Select Path1 
        From (Select 'One' Path1
              union
              Select 'Two' Path1
              union
              Select 'Three' Path1) T        
         FOR XML PATH('Image'),ROOT('Images'), ELEMENTS, TYPE) AS 'SomeParentNode', -- and here

         'Other value' as [Value]

FOR XML PATH('ItemRow'),TYPE,ELEMENTS  

输出:

<ItemRow>
  <Title>Somthing</Title>
  <SomeParentNode>
    <Notes>Some notes</Notes>
    <Images>
      <Image>
        <Path1>One</Path1>
      </Image>
      <Image>
        <Path1>Two</Path1>
      </Image>
      <Image>
        <Path1>Three</Path1>
      </Image>
    </Images>
  </SomeParentNode>
  <Value>Other value</Value>
</ItemRow>

答案 1 :(得分:1)

  1. 很长一段路,使用子查询将元素放在正确的路径中:
  2. SELECT
        'Something' AS Title,
        (
            SELECT
                'Some Notes' AS Notes,
                (
                    SELECT
                        Path1
                    FROM    
                        (VALUES('One'),('Two'),('Three')) AS T(Path1)
                    FOR 
                        XML PATH('Image'), TYPE
                )
            FOR 
                XML PATH('Images'), TYPE
        ),
        'Other value' as Value
    FOR
        XML PATH('ItemRow')
    
    1. 通过在字段名称中添加根元素名称来在适当位置选择元素的简短方法
    2. SELECT
          'Something' AS Title,
          'Some Notes' AS [Images/Notes],
          (
              SELECT
                  Path1
              FROM    
                  (VALUES('One'),('Two'),('Three')) AS T(Path1)
              FOR 
                  XML PATH('Image'), TYPE
          ) AS [Images],
          'Other value' as Value
      FOR
          XML PATH('ItemRow')
      

      两者都导致:

      <ItemRow>
        <Title>Something</Title>
        <Images>
          <Notes>Some Notes</Notes>
          <Image>
            <Path1>One</Path1>
          </Image>
          <Image>
            <Path1>Two</Path1>
          </Image>
          <Image>
            <Path1>Three</Path1>
          </Image>
        </Images>
        <Value>Other value</Value>
      </ItemRow>