XMLSERIALIZE is part of the SQL/XML standard. It takes an XML data type value that was previously constructed or parsed in SQL and serializes it into a textual (CHAR, VARCHAR, CLOB) or binary (BINARY, VARBINARY, BLOB) representation. You can specify whether the serialization should treat the value as a DOCUMENT (exactly one top-level element) or as CONTENT (any sequence of nodes). Optional formatting clauses such as INDENT control pretty-printing. The function is deterministic, respects character set and encoding rules of the target data type, and preserves namespaces and entity references. XMLSERIALIZE is commonly paired with XMLPARSE (to go from string to XML) and XMLQUERY/XQUERY functions for manipulation. Be aware that very large XML documents may exceed the maximum length of the chosen target type, and that some dialects support only character targets.
DOCUMENT or CONTENT
(keyword) - Defines whether the root must be a single element (DOCUMENT) or can be any XML node sequence (CONTENT).xml_expression
(XML) - The XML value to be serialized.target_sql_type
(SQL character or binary type) - Destination data type (e.g., VARCHAR(5000), CLOB, BLOB).INDENT
(optional keyword) - Requests pretty-printed output with line breaks and spaces.SIZE
(optional integer) - Number of spaces per indentation level when INDENT is used.XMLPARSE, XMLQUERY, XMLTABLE, XMLELEMENT, XMLATTRIBUTES
SQL:2003 (SQL/XML)
It turns an in-database XML value into a plain character or binary string so you can export, log, or further process the data.
Yes. Use the INDENT clause, optionally followed by SIZE n, to add line breaks and spaces that make the XML easier to read.
No. Validation occurs when the XML value is created. XMLSERIALIZE simply converts the in-memory representation to text.
DOCUMENT enforces a single root element, matching well-formed XML documents. CONTENT allows multiple top-level nodes, useful for fragments.