I am capturing data from the inserted and deleted tables as xml and saving it to an audit log but for some reason empty elements are begin generated for null values. This appears to contradict the documentation which says:
The ELEMENTS directive constructs XML in which each column value maps
to an element in the XML. If the column value is NULL, no element is
added. By specifying the optional XSINIL parameter on the ELEMENTS
directive, you can request that an element also be created for the
NULL value. In this case, an element that has the xsi:nil attribute
set to TRUE is returned for each NULL column value.
I’m quite certain I’m not using the XSINIL parameter but the elements are being created all the same. I’ve tried playing with the generation mode; trying RAW, AUTO, etc but the empty tags remain regardless.
Its a dynamically generated query that looks like:
'SET @LogData =
CASE
WHEN @EventType = @Insert
THEN
(
SELECT ' + @ColumnList + '
FROM Inserted
FOR XML RAW, ELEMENTS
)
WHEN @EventType = @Delete
THEN
(
SELECT ' + @ColumnList + '
FROM Deleted Rows
FOR XML RAW, ELEMENTS
)
END;'
Where @LogData is of type xml and @ColumnList is in the format:
[Column1Name],
[Column2Name]
Turns out the table I was testing had nearly every column (400+) set to a default constraint of an empty string, which resulted in those columns being included in the generated xml.
Other tables I tested did not have this default constraint so xml was generated as expected.