WHILE
ループを必要としないこれへのアプローチを探すと思いました。主な問題は、ノードとともにアイテムの位置を返すことですが、この投稿の最後の回答で回避策を見つけました: http://social.msdn.microsoft.com/Forums/nl/sqlxml/thread/46a7a90d-ebd6- 47a9-ac56-c20b72925fb3
したがって、代替アプローチは次のようになります。
insert into mytable ([Word])
select word from (
Select
words.value('@entry','nvarchar(max)') as word,
words.value('1+count(for $a in . return $a/../*[. << $a])', 'int') as Pos
from
OtherTable ot
cross apply ot.XMLColumn.nodes('words/word') x(words)
) sub where Pos <= @j
基本的に、内側のクエリは各単語とその位置を返します。これは外側のクエリでフィルタリングできます。
OtherWords
参考までに、テーブルの私の定義は次のとおりです。
create table OtherTable (XMLColumn xml)
insert OtherTable (XMLColumn)
select '<words><word entry="Wibble"/><word entry="Hello"/><word entry="World"/></words>'