When using FOR XML
in SQL Server, we can use the ELEMENTS
directive to include a column as an element instead of an attribute. However by default, if a column contains a NULL value, no element is produced for that column in the resulting XML document. This may or may not be what we want, depending on the requirements.
If we want such columns to be included in the XML even when they contain NULL values, all we need to do is include the XSINIL
option. This option specifies that any column that has a NULL value automatically gets an element with xsi:nil="true"
in the resulting XML.
The alternative is ABSENT
, which means columns with NULL values are excluded (this is the default behaviour).
Continue reading →