Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.0 (中文) » SQL Anywhere 服务器 - SQL 的用法 » 在数据库中使用 XML » 使用 SQL/XML 以 XML 形式获取查询结果

 

使用 XMLFOREST 函数

使用 XMLFOREST 可以构建 XML 元素林。对于每个 XMLFOREST 参数,都会生成一个元素。

以下查询会生成一个 <item_description> 元素,其中又包含有 <name>、<color> 和 <price> 元素:

SELECT ID, XMLELEMENT( NAME item_description,
                       XMLFOREST( Name as name,
                                  Color as color,
                                  UnitPrice AS price )
                      ) AS product_info
FROM Products
WHERE ID > 400;

此查询将生成以下结果:

ID product_info
401
<item_description>
 <name>Baseball Cap</name>
 <color>White</color>
 <price>10.00</price>
</item_description>
500
<item_description>
 <name>Visor</name>
 <color>White</color>
 <price>7.00</price>
</item_description>
501
<item_description>
 <name>Visor</name>
 <color>Black</color>
 <price>7.00</price>
</item_description>
... ...

有关详细信息,请参见XMLFOREST 函数 [String]