名称指定为通配符的列

如果指定的列名是一个通配符 (*),则插入此列的内容时就像没有指定列名那样插入。 如果此列不是 xml 类型的列,则此列的内容将作为文本节点插入,如下例所示:

USE AdventureWorks2012;  
GO  
SELECT E.BusinessEntityID "@EmpID",   
       FirstName "*",   
       MiddleName "*",   
       LastName "*"  
FROM   HumanResources.Employee AS E  
INNER JOIN Person.Person AS P  
    ON E.BusinessEntityID = P.BusinessEntityID  
WHERE E.BusinessEntityID=1  
FOR XML PATH;  

结果如下:

<row EmpID="1">KenJS??nchez</row>

如果此列的类型为 xml,则将插入相应的 XML 树。 例如,下面的查询将“*”指定为一个列的列名,该列包含由针对 Instructions 列的 XQuery 所返回的 XML。

SELECT   
       ProductModelID,  
       Name,  
       Instructions.query('declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"  
                /MI:root/MI:Location   
              ') as "*"  
FROM Production.ProductModel  
WHERE ProductModelID=7  
FOR XML PATH;   
GO  

结果如下: 插入 XQuery 所返回的 XML 时不带包装元素。

<row>

<ProductModelID>7</ProductModelID>

<Name>HL Touring Frame</Name>

<MI:Location LocationID="10">...</MI:Location>

<MI:Location LocationID="20">...</MI:Location>

...

</row>

另请参阅

将 PATH 模式与 FOR XML 一起使用