J
Yes, we did this type of thing at a company I worked at a few years ago. Even though we did it only for error logging, it was the same general concept. You just need to write the SELECT statement that includes all of the input parameters, use FOR XML, and store that in a variable that can be inserted into your Execution table in a new field: Parameters XML NULL. And then you can scrap the ExecutionParameter table.
Try the following as it shows three ways of generating the XML: Attribute-based, Element-based, and Element-based including NULL elements. The default behavior of FOR XML is to indicate NULL values by not including the attribute or element. But if you are doing Element-based XML and want the parameters to show even if they are NULL, then you need to specify XSINIL in the FOR XML clause. The ability to include an indicator for NULL is not available in Attribute-based XML.
Please note that there is not a huge difference between Attributed-based XML and the default Element-based XML (default = does not include NULL items) in terms of storage size (as shown in the example below). When viewing the XML it does appear that there is a lot more "bloat" with Element-based, and that would be the case if storing the data in an NVARCHAR field or text file. But the XML datatype uses an optimized method of storing the data internally, in a way that cannot be seen, but is clearly indicated in the resulting DATALENGTH output.
There is, however, a slight increase in size when using the XSINIL option for Element-based XML and it needs to keep track of NULL elements which are excluded from the other two types. The increase in size is due to a one-time hit for declaring the xsi namespace in the root element, and then a per-NULL element hit.
DECLARE @Param1 INT = 5,
@Param2 DATETIME = NULL,
@Param3 NVARCHAR(50) = N'Test < some & XML chars "',
@Param4 DATETIME = '2016-04-09';
DECLARE @Parameters XML;
SELECT @Parameters = (
SELECT @Param1 AS [Param1],
@Param2 AS [Param2],
@Param3 AS [Param3],
@Param4 AS [Param4]
FOR XML RAW(N'Params') -- Attribute-based XML (NULL attributes are missing)
);
SELECT @Parameters, DATALENGTH(@Parameters); -- 183
SELECT @Parameters = (
SELECT @Param1 AS [Param1],
@Param2 AS [Param2],
@Param3 AS [Param3],
@Param4 AS [Param4]
FOR XML RAW(N'Params'), ELEMENTS -- Element-based XML (NULL elements are missing)
);
SELECT @Parameters, DATALENGTH(@Parameters); -- 185
SELECT @Parameters = (
SELECT @Param1 AS [Param1],
@Param2 AS [Param2],
@Param3 AS [Param3],
@Param4 AS [Param4]
FOR XML PATH(N'Params'), ELEMENTS XSINIL -- Element-based XML (NULL elements included)
);
SELECT @Parameters, DATALENGTH(@Parameters); -- 434
Returned XML values:
5
Test < some & XML chars "
2016-04-09T00:00:00
5
Test < some & XML chars "
2016-04-09T00:00:00
Additional notes:
For SchemaName and ProcedureName columns, I would use sysname as the datatype since that is how it is defined in the system tables. And be sure to use lower-case for sysname as it is an alias that lives in master and servers with a default collation that is binary or case-sensitive won't be able to find that alias if it is not all lower-case. Your current server might be case-insensitive, but it is still a good habit to get into when using sysname.
For the ExecutionError table, be sure to include INT columns to capture: ERROR_LINE(), ERROR_NUMBER(), ERROR_STATE(), and ERROR_SEVERITY().
No, you don't want to use SQL_VARIANT as that prevents you from storing the LOB types: VARCHAR(MAX), NVARCHAR(MAX), VARBINARY(MAX), and XML.
No, you can't use NVARCHAR(8000) because NVARCHAR has a top size of 4000, unless you use MAX.