I have been asked many times why the DBA specification requires that the text () function be added to the XPath of the value method when parsing XML values, it seems that the result of adding and not adding is the same.
The following test script shows the difference between text () and why it is required.
Declare
@ XML XML
Set @ XML = N'
<A/>
<B> B
<C> C </C>
</B>' ;
Select @ XML . Value ( '(/A) [1]' , 'Varchar (10 )' );
Select @ XML . Value ( '(/A/text () [1]' , 'Varchar (10 )' );
Select @ XML . Value ( '(/B) [1]' , 'Varchar (10 )' );
Select @ XML . Value ( '(/B/text () [1]' , 'Varchar (10 )' );
Execute this script. The obvious difference is displayed in the result.
By comparing the execution plans, we can see that there are significant differences between the two implementation plans and significant differences in efficiency. If text () is not used, additional steps are required to retrieve the data of the child node (even if the child node exists, the overhead still exists ).
Therefore, if it is not for a special purpose, you should specify the text () function in XPath for precise positioning to reduce performance overhead.