次の方法で共有


XQuery での文字列検索

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

使用例

A. 製品カタログから "maintenance" という語を含む機能説明を検索する

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

上のクエリは、FLOWR 式の where により、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)