5

私はそのようなデータ構造を持っています:

<rootnode>
  <group>
    <id>1</id>
     <anothernode>first string</anothernode>
     <anothernode>second string</anothernode>
  </group>
 <group>
   <id>2</id>
     <anothernode>third string</anothernode>
     <anothernode>fourth string</anothernode>
  </group>
</rootnode>

そして、次のコード:

EXEC sp_xml_preparedocument @index OUTPUT, @XMLdoc

SELECT *
FROM OPENXML (@index, 'rootnode/group')
WITH 
(
  id int 'id',
  anothernode varchar(30) 'anothernode'
)

結果が得られます

id | anothernode
————————————————
1  | first string
2  | third string

代わりに 4 つの文字列すべてが表示されている代わりに、この結果を表示するにはどうすればよいですか?

id | anothernode
————————————————
1  | first string
1  | second string
2  | third string
2  | fourth string
4

1 に答える 1

11
SELECT *
FROM OPENXML (@index, 'rootnode/group/anothernode')
WITH 
(
  id int '../id',
  anothernode varchar(30) '.'
)

または、次のように代わりにXMLデータ型を使用できます。

SELECT G.N.value('(id/text())[1]', 'int') AS id,
       A.N.value('text()[1]', 'varchar(30)') AS anothernode
FROM @XMLDoc.nodes('rootnode/group') AS G(N)
  CROSS APPLY G.N.nodes('anothernode') AS A(N)
于 2011-11-04T20:36:59.510 に答える