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)