返回的 XML 成形过程中的 AUTO 模式试探方法AUTO Mode Heuristics in Shaping Returned XML

适用于: SQL Server Azure SQL 数据库 Azure Synapse Analytics 并行数据仓库

AUTO 模式根据查询决定返回的 XML 的形式。AUTO mode determines the shape of returned XML based on the query. 在决定嵌套元素的方式时,AUTO 模式试探方法会比较相邻行中的列值。In determining how elements are to be nested, AUTO mode heuristics compare column values in adjacent rows. ntexttextimagexml 类型以外的所有类型的列都会进行比较。Columns of all types, except ntext, text, image, and xml, are compared. (n)varchar(max)varbinary(max) 类型的列会进行比较。Columns of type (n)varchar(max) and varbinary(max) are compared.

下面的示例说明了确定生成的 XML 的形式的 AUTO 模式试探方法:The following example illustrates the AUTO mode heuristics that determine the shape of the resulting XML:

SELECT T1.Id, T2.Id, T1.Name  
FROM   T1, T2  
WHERE ...  
ORDER BY T1.Id
FOR XML AUTO;

如果未指定表 T1 的键,若要确定新 <T1> 元素的开始位置,需要比较 T1 中除 ntext text imagexml 类型以外的所有列的值。To determine where a new <T1> element starts, all column values of T1, except ntext, text, image and xml, are compared if the key on the table T1 is not specified. 接下来,假定 Name 列的数据类型为 nvarchar(40) ,SELECT 语句将返回如下行集:Next, assume that the Name column is nvarchar(40) and the SELECT statement returns this rowset:

T1.Id  T1.Name  T2.Id  
-----------------------  
1       Andrew    2  
1       Andrew    3  
1       Nancy     4  

AUTO 模式试探方法将比较表 T1 的所有值(Id 列和 Name 列)。The AUTO mode heuristics compare all the values of table T1, the Id and Name columns. 因为前两行的 Id 列和 Name 列具有相同的值,所以向结果中添加了一个具有两个 <T2> 子元素的 <T1> 元素。Because the first two rows have the same values for the Id and Name columns, one <T1> element having two <T2> child elements is added in the result.

下面是返回的 XML:Following is the XML that is returned:

<T1 Id="1" Name="Andrew">  
    <T2 Id="2" />  
    <T2 Id="3" />  
</T1>  
<T1 Id="1" Name="Nancy" >  
      <T2 Id="4" />  
</T>  

现在,假定 Name 列是 text 类型。Now assume that the Name column is of text type. AUTO 模式试探方法不比较此类型的值,The AUTO mode heuristics do not compare the values for this type. 而是认为这些值不相同。Instead, it assumes that the values are not the same. 这将产生如下所示的 XML 生成结果:This results in XML generation as shown in the following:

<T1 Id="1" Name="Andrew" >  
  <T2 Id="2" />  
</T1>  
<T1 Id="1" Name="Andrew" >  
  <T2 Id="3" />  
</T1>  
<T1 Id="1" Name="Nancy" >  
  <T2 Id="4" />  
</T1>  

另请参阅See Also

将 AUTO 模式与 FOR XML 一起使用Use AUTO Mode with FOR XML