EXPLICIT mode
Applies to ❌ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
The EXPLICIT
mode generates XML content based on the "explicit" instructions on how to nest content, and thus provides the most flexibility in SQL Server's syntax, which even the authors of the syntax have to look up constantly, themselves.
Consider the following query
SELECT 1 [Tag], null [Parent], book.id [Book!1!BookID] FROM book ORDER BY id FOR XML PATH
create.select( inline(1).as("Tag"), inline((Integer) null).as("Parent"), BOOK.ID.as("Book!1!BookID")) .from(BOOK) .orderBy(BOOK.ID) .forXML().explicit() .fetch();
This query produces a document fragment like this:
<Book BookID="1"/> <Book BookID="2"/> <Book BookID="3"/> <Book BookID="4"/>
Dialect support
This example using jOOQ:
select( inline(1).as("Tag"), inline((Integer) null).as("Parent"), BOOK.ID.as("Book!1!BookID")) .from(BOOK) .orderBy(BOOK.ID) .forXML().path()
Translates to the following dialect specific expressions:
DB2, Oracle
SELECT xmlagg(xmlelement( NAME row, xmlelement(NAME Tag, Tag), xmlelement(NAME Parent, Parent), xmlelement(NAME Book!1!BookID, Book!1!BookID) )) FROM ( SELECT 1 Tag, NULL Parent, BOOK.ID Book!1!BookID FROM BOOK ORDER BY BOOK.ID ) t
Postgres
SELECT xmlagg(xmlelement( NAME row, xmlelement(NAME Tag, Tag), xmlelement(NAME Parent, Parent), xmlelement(NAME Book!1!BookID, Book!1!BookID) )) FROM ( SELECT 1 Tag, CAST(NULL AS int) Parent, BOOK.ID Book!1!BookID FROM BOOK ORDER BY BOOK.ID ) t
SQLServer
SELECT ( SELECT 1 Tag, NULL Parent, BOOK.ID Book!1!BookID FROM BOOK ORDER BY BOOK.ID FOR XML PATH )
Teradata
SELECT xmlagg(xmlelement( NAME row, xmlelement(NAME Tag, Tag), xmlelement(NAME Parent, Parent), xmlelement(NAME Book!1!BookID, Book!1!BookID) )) FROM ( SELECT * FROM ( SELECT TOP 999999999999999999 1 Tag, NULL Parent, BOOK.ID Book!1!BookID FROM BOOK ORDER BY BOOK.ID ) x ) t
ASE, Access, Aurora MySQL, Aurora Postgres, BigQuery, ClickHouse, CockroachDB, Databricks, Derby, DuckDB, Exasol, Firebird, H2, HSQLDB, Hana, Informix, MariaDB, MemSQL, MySQL, Redshift, SQLDataWarehouse, SQLite, Snowflake, Sybase, Trino, Vertica, YugabyteDB
/* UNSUPPORTED */
Generated with jOOQ 3.21. Support in older jOOQ versions may differ. Translate your own SQL on our website
Feedback
Do you have any feedback about this page? We'd love to hear it!