// strings reference

SQL XMLQUERY function

XMLQUERY('/order/total' PASSING doc RETURNING CONTENT) returns the matching XML fragment from an XMLType value. Only Oracle implements the SQL/XML function itself.

카테고리
Strings
Returns
XML
In this category
163 of 172

구문

XMLQUERY(xquery_string PASSING column RETURNING CONTENT)  -- Oracle
column.query('xpath')  -- SQL Server

Each block is a separate form. The support table and the examples show which engine takes which.

엔진 지원

엔진Support메모
SQL Server Different name or syntax the .query() method over xml columns
PostgreSQL Different name or syntax xml type with xpath(), not the XMLQUERY function
MySQL Not built in no XML type; ExtractValue(xml, xpath) returns text from an XPath
SQLite Not built in no XML support
Oracle 예 Supported under this name.

예시

Oracle

SELECT XMLQUERY('/order/total/text()'
  PASSING doc RETURNING CONTENT) AS total
FROM orders_xml;

SQL Server

SELECT doc.query('/order/total') AS total
FROM orders_xml;

알아 두면 좋은 정보

  • Support varies widely: Oracle and SQL Server have full XQuery, PostgreSQL has XPath 1.0 over its xml type, MySQL has ExtractValue() and UpdateXML() over plain strings, and SQLite has nothing.
  • Oracle's XMLQUERY returns XML; add RETURNING CONTENT and XMLCAST for scalars. SQL Server splits the roles: .query() returns fragments, .value() extracts scalars.
  • PostgreSQL has an xml type with xpath() (XPath 1.0 only), capable for small jobs, short of the full SQL/XML XMLQUERY surface.

Questions about XMLQUERY

Should I store XML in the database?

Only when the format is fixed by someone else. Every engine here has JSON functions, while SQLite has no XML support and MySQL's is limited to XPath over strings. If you control the format, store JSON.

Why does xpath() return an array on PostgreSQL?

xpath() returns xml[], a set of nodes. Take subscript [1] for a single node, and remember it is XPath 1.0: no XQuery expressions like if/then.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.