Share via


XQuery での文字列検索

適用対象:SQL Server

このトピックには、XML ドキュメント内のテキストを検索する方法を示すサンプル クエリを掲載しています。

A. 製品カタログの "メンテナンス" という単語を含む機能の説明を検索する

SELECT CatalogDescription.query('  
     declare namespace p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";  
    for $f in /p1:ProductDescription/p1:Features/*  
     where contains(string($f), "maintenance")  
     return  
           $f ') as Result  
FROM Production.ProductModel  
WHERE ProductModelID=19  

前のクエリでは、 where FLOWR 式の が式の結果を for フィルター処理し、 contains() 条件を満たす要素のみを返します。

結果を次に示します。

<p1:Maintenance     
      xmlns:p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">  
 <p1:NoOfYears>10</p1:NoOfYears>  
 <p1:Description>maintenance contact available through your   
               dealer or any AdventureWorks retail store.</p1:Description>  
</p1:Maintenance>  

参照

XML データ (SQL Server)
XQuery 言語リファレンス (SQL Server)