XMLFOREST is part of the SQL/XML standard. It takes one or more SQL expressions and returns a single XML value that contains a sequence (forest) of XML elements. For every expression supplied, the function creates an element whose name is either the column alias or, if none is provided, the column name. Null expressions are omitted from the output, keeping the XML compact. The order of elements in the result matches the order of arguments in the call. XMLFOREST is typically used with XMLSERIALIZE or direct client consumption to convert relational row data into well-formed XML that can be exchanged with external systems. Because it is evaluated per row, using it in a SELECT list returns one XML fragment per row. When the target column is already of type XML, the result can be stored directly; otherwise, it can be cast to text. Performance may be affected by large result sets, so indexing or limiting row counts is advised when generating large XML payloads.
exprN
- any SQL expression that resolves to a scalar valuealiasN
- optional identifier used as the XML element name; defaults to the expression or column name if omittedXMLELEMENT, XMLAGG, XMLATTRIBUTES, XMLSERIALIZE, XMLTABLE, FOR XML
SQL:2003 (SQL/XML functional extensions)
SQL XMLFOREST takes one or more expressions and turns each into a sibling XML element, returning the combined XML fragment.
Supply an alias after the expression, for example `XMLFOREST(empno AS "EmpId")`, to control the element name.
No. If an expression evaluates to NULL, XMLFOREST omits that element entirely, preventing empty tags.
Yes. Wrap XMLFOREST inside XMLAGG, then optionally surround it with XMLELEMENT to add a root node, and finally serialize with XMLSERIALIZE.