If you’re getting error msg 6825 that reads “ELEMENTS option is only allowed in RAW, AUTO, and PATH modes of FOR XML“, it’s probably because you’re trying to use the ELEMENTS
directive while using EXPLICIT
mode of the FOR XML
clause.
As the message alludes to, this option is not available when using the EXPLICIT
mode (it’s only allowed with the RAW
, AUTO
, and PATH
modes).
However, SQL Server does provide us with an equivalent that provides the same result. When using EXPLICIT
mode, we can use the ELEMENT
directive instead. We apply this to the column names, instead of the end of the query. We can alternatively use the ELEMENTXSINIL
directive to ensure that elements are generated even if the column contains a NULL value.
Continue reading →