【问题标题】:Sql - FOR XML Path query issueSql - FOR XML 路径查询问题
【发布时间】:2016-10-28 13:32:34
【问题描述】:

我有如下查询(示例)。

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>

我正在尝试将注释和图像放入父节点,所以它应该如下所示

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

这可能吗?

【问题讨论】:

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


    【解决方案1】:

    只需像这样添加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>
    

    【讨论】:

      【解决方案2】:
      1. 很长的路要走,使用子查询将元素放置在正确的路径中:
      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. 快捷方式,通过在字段名称中添加根元素名称来选择适当位置的元素
      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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-20
        • 1970-01-01
        相关资源
        最近更新 更多