The warning is there because of the XML function value()
. The second parameter to value()
is what you want the value stored in the XML to be converted to. You could argue that this is not in fact an implicit conversion but a very explicit conversion since you are asking for it to happen. Perhaps something for a connect item to suggest to Microsoft.
Simplest way to reproduce what you see.
declare @X xml;
select @X.value('text()[1]', 'int');
Gives these two warnings.
<Warnings>
<PlanAffectingConvert ConvertIssue="Cardinality Estimate" Expression="CONVERT_IMPLICIT(int,XML Reader with XPath filter.[lvalue],0)" />
<PlanAffectingConvert ConvertIssue="Cardinality Estimate" Expression="CONVERT_IMPLICIT(int,XML Reader with XPath filter.[value],0)" />
</Warnings>
As you can see you get it with int's as well and you get two of them for each invocation of value()
.
declare @X xml;
select @X.value('text()[1]', 'int'),
@X.value('text()[1]', 'bit');
<Warnings>
<PlanAffectingConvert ConvertIssue="Cardinality Estimate" Expression="CONVERT_IMPLICIT(int,XML Reader with XPath filter.[lvalue],0)" />
<PlanAffectingConvert ConvertIssue="Cardinality Estimate" Expression="CONVERT_IMPLICIT(int,XML Reader with XPath filter.[value],0)" />
<PlanAffectingConvert ConvertIssue="Cardinality Estimate" Expression="CONVERT_IMPLICIT(bit,XML Reader with XPath filter.[lvalue],0)" />
<PlanAffectingConvert ConvertIssue="Cardinality Estimate" Expression="CONVERT_IMPLICIT(bit,XML Reader with XPath filter.[value],0)" />
</Warnings>
The conversion is done in the Stream Aggregate operator that calculate the value like this.
MIN(CASE WHEN [@X] IS NULL
THEN NULL
ELSE
CASE WHEN datalength(XML Reader with XPath filter.[value])>=(128)
THEN CONVERT_IMPLICIT(int,XML Reader with XPath filter.[lvalue],0)
ELSE CONVERT_IMPLICIT(int,XML Reader with XPath filter.[value],0)
END
END)
The result from the Table Valued function is returned in the lvalue
or value
column. The expression checks by using datalength
to figure out where it should be fetched from and then converts it to your desired datatype.
Is there any way to remove this warning?
Yes there is. You remove the TYPE
directive from the FOR XML PATH
statement and you remove the call to the value()
function. A side effect of doing that is that values you concatenate that contains characters that needs to be encoded in the XML like &<>
will be encoded in your result.