SQL SERVER中XML查询:FOR XML指定AUTO
前言
在SQL SERVER中,XML查询可以指定RAW,AUTO,EXPLICIT,PATH。本文用一些实例介绍SQL SERVER中指定AUTO的XML查询。
基础示例
with TestXml
as
(
select 1 as id,N'LeeWhoeeUniversity' as name
union all
select 2,N'DePaul'
union all
select 3 ,null
)
select id,name from testxml for xml auto
结果:
<testxml id="1" name="LeeWhoeeUniversity" />
<testxml id="2" name="DePaul" />
<testxml id="3" />
用表名做元素名称,即替代RAW模式中的“row”。
下面看多表的查询(片断2):
with [order]
as
(
select 122 as orderid, 1 as productid,10 as quantity
union all
select 123,1 as productid,100 as quantity
union all
select 124,2,20
union all
select 125,3 ,5
),
product
as
(
select 1 as id,N'LeeWhoeeUniversity' as name
union all
select 2,N'DePaul'
)
select * from product,[order] where [order].productid=product.id for xmlauto
结果:
<product id="1" name="LeeWhoeeUniversity">
<order orderid="122" productid="1" quantity="10" />
<order orderid="123" productid="1" quantity="100" />
</product>
<product id="2" name="DePaul">
<order orderid="124" productid="2" quantity="20" />
</product>
表名顺序敏感
如果把product和order换一下位置,片断3:
with [order]
as
(
select 122 as orderid, 1 as productid,10 as quantity
union all

本文详细介绍了SQL SERVER中FOR XML AUTO查询模式的使用,通过基础示例、表名作为元素名称、顺序敏感性、处理text类型以及排序影响等方面进行探讨,展示了XML查询在不同场景下的生成结果。
最低0.47元/天 解锁文章
197

被折叠的 条评论
为什么被折叠?



