【问题标题】:one xml element (with children) to many table rows一个 xml 元素(带子元素)到许多表行
【发布时间】:2012-09-19 19:01:44
【问题描述】:

我需要把下面的XML结构变成下面的表结构(在SQL Server 2000中)

<family>
  <parent>
    <id>1</id>
    <child>test1</child>
    <child>test2</child>
  </parent>
  <parent>
    <id>2</id>
    <child>test3</child>
    <child>test4</child>
  </parent>
</family>

表格中的样本数据:

parent_id child
1         test1
1         test2
2         test3
2         test4

查询:

SELECT Child FROM 
  OPENXML (@hdoc, 'family/parent/child') 
    WITH(Child nvarchar(256) '.')

给出结果:

test1
test2
test3
test4

查询 #2:

SELECT ParentID, Child FROM 
  OPENXML (@hdoc, 'family/parent') 
    WITH(
      Child nvarchar(256) 'child/.',
      ParentID int 'id/.'
    )

给出结果:

1 test1
2 test3

如何获取父 ID 和所有子 ID?

【问题讨论】:

    标签: xml sql-server-2000


    【解决方案1】:
    SELECT ParentID, Child FROM 
      OPENXML (@hdoc, 'family/parent/child') 
        WITH(
          Child nvarchar(256) '.',
          ParentID int '../id'
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 2017-03-23
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      相关资源
      最近更新 更多