2017-10-30 1 views
1

次のSQLは、2番目と3番目の列のすべての行に対して1を返します。しかし、それはちょうど最初の行のために1を返すべきではありませんか?XMLが特定の文字列であるかどうかを確認しますか?

with t as (select cast('SomeText' as xml) x 
       union all 
       select cast('<s>Test</s>' as xml) 
      ) 
    select * , 
      x.exist('contains(/text()[1], "SomeText.")') , 
      x.exist('/text() = "SomeText."') 
    from t 
    where x.exist('contains(/text()[1], "SomeText.")') = 1 
      and x.exist('/text() = "SomeText."') = 1; 

私はx.value('/', 'nvarchar(max)') = 'SomeText.'を使用して問題を解決できることを知っています。

XQuery x.exist('/text() = "SomeText."')またはx.exist('contains(/text()[1], "SomeText.")')が機能していない(常にtrueを返す)理由については、質問があります。

答えて

2

falseの結果がまだ存在しないので、私はあなたの元のコードは、これらの行を返して想像

+-------------+------------------+------------------+------------------+------------------+ 
|  x  | (No column name) | (No column name) | (No column name) | (No column name) | 
+-------------+------------------+------------------+------------------+------------------+ 
| SomeText | false   | false   |    0 |    0 | 
| SomeText. | true    | true    |    1 |    1 | 
| <s>Test</s> | false   | false   |    0 |    0 | 
+-------------+------------------+------------------+------------------+------------------+ 

を返し

with t as ( select cast('SomeText' as xml) x 
       union all 
       select cast('SomeText.' as xml) x 
       union all 
       select cast('<s>Test</s>' as xml) 
      ) 
    select * , 
      x.query('contains(/text()[1], "SomeText.")') , 
      x.query('/text() = "SomeText."'), 
      x.exist('.[contains(/text()[1], "SomeText.")]') , 
      x.exist('.[text()[1] eq "SomeText."]') 
    from t 

を試してみてください。

declare @x xml; 
set @x=''; 
select @x.exist('false()'); 

はまだ1

+0

感謝を返すん。条件は '.exist()'に対して '。[....]'で囲む必要があります。 – ca9163d9

関連する問題