9

このエラーが発生しないようにクエリを変更する方法:

XML データ型メソッド「値」は文字列リテラルでなければなりません

T-SQL コード:

Declare @Count Int = 1 
While(@count <= @j) 
Begin 
insert into mytable 
([Word]) 
Select ([XmlColumn].value(N'word['+Cast(@Count as nvarchar(2))+']/@Entry','nvarchar(max)')) 
    from OtherTable WHERE ID=2
4

2 に答える 2

1

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>'
于 2012-06-14T09:32:29.723 に答える