SQL SERVER中XML查询:FOR XML指定AUTO
2014-04-04 02:05
507 查看
SQL SERVER中XML查询:FOR XML指定AUTO
前言
在SQL SERVER中,XML查询可以指定RAW,AUTO,EXPLICIT,PATH。本文用一些实例介绍SQL SERVER中指定AUTO的XML查询。基础示例
片断1: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
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 [order],product where [order].productid=product.id for xml auto
结果:
<order orderid="122" productid="1" quantity="10">
<product id="1" name="LeeWhoeeUniversity" />
</order>
<order orderid="123" productid="1" quantity="100">
<product id="1" name="LeeWhoeeUniversity" />
</order>
<order orderid="124" productid="2" quantity="20">
<product id="2" name="DePaul" />
</order>
当然,AUTO模式同样也可以指定ELEMENTS,BINARY BASE64,同RAW。(SQL
SERVER中XML查询:FOR XML指定RAW)
返回的 XML 成形过程中的 AUTO 模式试探方法
AUTO 模式根据查询决定返回的 XML 的形式。 在决定嵌套元素的方式时,AUTO 模式试探方法会比较相邻行中的列值。ntext、text、image 和xml 类型以外的所有类型的列都会进行比较。 (n)varchar(max) 和varbinary(max) 类型的列也会进行比较。上面的第一个指定AUTO的SQL语句(片断2)结果集为:
id name orderid productid quantity
1 LeeWhoeeUniversity 122 1 10
1 LeeWhoeeUniversity 123 1 100
2 DePaul 124 2 20
AUTO 模式试探方法将比较表 product 的所有值(Id 列和 Name 列)。因为前两行的 Id 列和 Name 列具有相同的值,所以向结果中添加了一个具有两个 <order> 子元素的 <product> 元素。
<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>
text类型的特殊
如果把Name 列改为 text 类型。 AUTO 模式试探方法不比较此类型的值, 而是认为这些值不相同。见下面代码片断4:
declare @order table(orderid int,productid int,quantity int)
declare @product table(id int,name text)
insert into @order
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
insert into @product
select 1 ,N'LeeWhoeeUniversity'
union all
select 2,N'DePaul'
select * from @product as product,@order as [order] where [order].productid=product.id for xmlauto
结果:
<product id="1" name="LeeWhoeeUniversity">
<order orderid="122" productid="1" quantity="10" />
</product>
<product id="1" name="LeeWhoeeUniversity">
<order orderid="123" productid="1" quantity="100" />
</product>
<product id="2" name="DePaul">
<order orderid="124" productid="2" quantity="20" />
</product>
上面结果中name同为LeeWhoeeUniversity的项被分成两个product。
结果集排序对AUTO试探的影响
再看第一个指定AUTO的SQL语句,但是更改了orderid为使其结果集中相同id和name的项不连在一起:with [order]
as
(
select 122 as orderid, 1 as productid,10 as quantity
union all
select 125,1 as productid,100 as quantity
union all
select 123,2,20
union all
select 124,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
order by orderid
结果:
id name orderid productid quantity
1 LeeWhoeeUniversity 122 1 10
2 DePaul 123 2 20
1 LeeWhoeeUniversity 125 1 100
然后进行指定AUTO的XML查询(即语句上添加for xml auto),AUTO模式试探将生成以下结果:
<product id="1" name="LeeWhoeeUniversity">
<order orderid="122" productid="1" quantity="10" />
</product>
<product id="2" name="DePaul">
<order orderid="123" productid="2" quantity="20" />
</product>
<product id="1" name="LeeWhoeeUniversity">
<order orderid="125" productid="1" quantity="100" />
</product>
这样相同id和name的product没有连在一起。
总结
以上对指定AUTO的XML查询就介绍完了,下一篇文章将继续用实例介绍SQL SERVER中的XML查询:指定EXPLICIT查询。SQL SERVER中XML查询:FOR XML指定RAW
SQL SERVER中XML查询:FOR XML指定AUTO
SQL SERVER中XML查询:FOR XML指定EXPLICIT
SQL SERVER中XML查询:FOR XML指定PATH
关于XML类型,请参考:http://blog.csdn.net/leewhoee/article/details/8571286
关于XML索引,请参考:http://blog.csdn.net/leewhoee/article/details/8579743
相关文章推荐
- SQL SERVER中XML查询:FOR XML指定AUTO
- SQL SERVER中XML查询:FOR XML指定AUTO
- SQL SERVER中XML查询:FOR XML指定RAW
- SQL SERVER中XML查询:FOR XML指定PATH
- SQL SERVER中XML查询:FOR XML指定PATH
- SQL SERVER中XML查询:FOR XML指定EXPLICIT
- SQL Server2005中使用XML-FOR XML(AUTO模式查询)
- SQL SERVER中 用FOR XML PATH将查询结果以XML输出 然后应用于 sql 合并字段功能介绍
- FOR XML AUTO将数据库表的一个字段的数据查询拼接为带间隔符的字符串
- 使用 FOR XML PATH 合并SQL Server查询结果的重复行
- SQL Server 2008 R2——使用FOR XML PATH实现多条信息按指定格式在一行显示
- sql server 在当前数据库查询 指定字符串,从而得出 该字符存在在 哪个表哪个字段
- TinyXml 查询指定节点、删除指定节点和增加节点
- sql server 使用for xml path 将1列多行转换为字符串连接起来
- for xml path以及sql合并查询
- SQL Server XML查询工具(附源码)
- 查询 SQL_Server 所有表的记录数: for xml path
- SQL Server XML基础学习<2>之--FOR XML AUTO/RAW
- 超级简单:使用FOR XML AUTO控制XML输出
- 合并多行查询数据到一行:使用自连接、FOR XML PATH('')、STUFF或REPLACE函数