XQuery query() メソッド を使用するサンプル

環境
SQL Server 2022

概要
xml 型の変数または列に格納されている XML インスタンスに対してクエリを実行するには、
xml データ型メソッドを使用します。

操作例1
xml 型の変数を宣言し、xml データ型の query() メソッドを使用してクエリを実行できます。

SQL構文

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
DECLARE @x xml
SET @x = '<ROOT><a>12345</a></ROOT>'
SELECT @x.query('/ROOT/a')
DECLARE @x xml SET @x = '<ROOT><a>12345</a></ROOT>' SELECT @x.query('/ROOT/a')
DECLARE @x xml
SET @x = '<ROOT><a>12345</a></ROOT>'
SELECT @x.query('/ROOT/a')

操作例2
AdventureWorks データベースの ProductModel テーブルの xml 型の Instructions 列に対してクエリを指定します。

SQL構文

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT Instructions.query('declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
/AWMI:root/AWMI:Location[@LocationID=10]
') as Result
FROM Production.ProductModel
WHERE ProductModelID=7
SELECT Instructions.query('declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"; /AWMI:root/AWMI:Location[@LocationID=10] ') as Result FROM Production.ProductModel WHERE ProductModelID=7
SELECT Instructions.query('declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";           
    /AWMI:root/AWMI:Location[@LocationID=10]  
') as Result   
FROM  Production.ProductModel  
WHERE ProductModelID=7

 

XQuery

Posted by arkgame