FOR XML 子句的基本语法
在FOR子句中指定XML模式的基本语法为:
FOR XML mode [, XMLDATA] [, ELEMENTS][, BINARY BASE64]
参数
XML mode
指定XML模式。XML模式决定所得到的XML的形式。
mode可以是RAW、AUTO或EXPLICIT。
XMLDATA
指定应返回XML-Data架构。文档的架构被预先设计为内嵌式架构。
ELEMENTS
如果指定ELEMENTS选项,则列作为子元素返回。否则,列将映射到XML特性。只有在AUTO模式下才支持该选项。
BINARY BASE64
如果指定 BINARY Base64 选项,则查询所返回的任何二进制数据都用 base64 编码格式表示。使用RAW和EXPLICIT模式检索二进制数据时,必须指定该选项。在AUTO模式中,默认情况下将二进制数据作为引用返回。
===========================================
1.FOR XML RAW
2.FOR XML AUTO
3.FOR XML EXPLICIT
4.更改显示Tag为中文
5.同表 多层
6.异表 多层
7.处理数据及日期等的Null值
==================
数据库Person表中的数据为
personName personAge
lisi 30
zhangsan 30
1. ----------[ RAW ]---------
SELECT [personName], [personAge]
FROM [TestDB].[dbo].[person]
FOR XML RAW
结果:
<row personName="lisi " personAge="30"/>
<row personName="zhangsan " personAge="30"/>
2.----------[ AUTO ]--------
SELECT [personName], [personAge]
FROM [TestDB].[dbo].[person]
FOR XML AUTO
结果:
<TestDB.dbo.person personName="lisi " personAge="30"/>
<TestDB.dbo.person personName="zhangsan " personAge="30"/>
3.-----------[ EXPLICIT ]--------
select 1 as Tag ,null as parent
,RTRIM(personName) as [PersonBasic!1!personName]
,RTRIM(personAge) as [PersonBasic!1!personAge!xml]
FROM [TestDB].[dbo].[person]
FOR XML EXPLICIT
结果:
<PersonBasic personName="lisi">
<personAge>30</personAge>
</PersonBasic>
<PersonBasic personName="zhangsan">
<personAge>30</personAge>
</PersonBasic>
4.----- 更改显示Tag为中文 -----
select
1 as tag,
null as parent,
personName as [人员!1!姓名!xml],
personAge as [人员!1!年龄!xml]
from person
for xml EXPLICIT
结果:
<人员>
<姓名>lisi </姓名>
<年龄>30</年龄>
</人员>
<人员>
<姓名>zhangsan </姓名>
<年龄>30</年龄>
</人员>
5. ----- 同表 多层 ----
select
1 as tag,
null as parent,
rtrim(A.personName) as [人员!1!姓名],
null as [人员信息!2!年龄!xml]
from person A
union all
select
2 as tag,
1 as parent,
rtrim(A.personName),
B.personAge
from person B,person A
where A.personName=B.personName
order by [人员!1!姓名],tag
for xml EXPLICIT
结果:
<人员 姓名="lisi">
<人员信息>
<年龄>30</年龄>
</人员信息>
</人员>
<人员 姓名="zhangsan">
<人员信息>
<年龄>30</年龄>
</人员信息>
</人员>
6.-------- 异表 多层 -------
select
1 as tag,
null as parent,
rtrim(A.personName) as [人员!1!姓名],
null as [人员信息!2!年龄!xml],
null as [人员信息!2!职业!xml]
from person A
union all
select
2 as tag,
1 as parent,
rtrim(A.personName),
B.personAge,
rtrim(B.personJob)
from personInfo B,person A
where B.personName=A.personName
order by [人员!1!姓名],tag
for xml EXPLICIT
结果:
<人员 姓名="lisi">
<人员信息>
<年龄>30</年龄>
<职业>teacher</职业>
</人员信息>
</人员>
<人员 姓名="zhangsan">
<人员信息>
<年龄>30</年龄>
<职业>worker</职业>
</人员信息>
</人员>
7.-------处理数据及日期等的Null值-------
数据库Person表中的数据为
personName personAge personBirth(可为空)
lisi 30 1987-06-06
zhangsan 30
当数据表中的字段为Null值时
产生的xml文档中 将没有该节点
为了解决这一问题
在必要的时候 可以将数字及日期类型
转换为字符串类型
这样就可以接收空串
(但不知实际运用中效果如何)
SELECT
1 as TAG,
null as parent,
RTRIM(personName) AS [人员!1!姓名!xml],
RTRIM(ISNULL(CONVERT(CHAR,personAge),'')) AS [人员!1!年龄!xml],
RTRIM(ISNULL(CONVERT(CHAR(10),personBirth,120),'')) AS [人员!1!出生日期!xml]
FROM person
FOR XML EXPLICIT
结果:
<人员>
<姓名>lisi</姓名>
<年龄>30</年龄>
<出生日期>1987-06-06</出生日期>
</人员>
<人员>
<姓名>zhangsan</姓名>
<年龄></年龄>
<出生日期></出生日期>
</人员>
转自:http://www.cnblogs.com/freeliver54/archive/2007/03/22/683563.html
====================================
以通用数据库Northwind为例,下列T-SQL语句可以用XML格式输出Categories中的ID和Name列表,并为其加上根节点ROOT:
select top 1
1 as Tag,
NULL as Parent,
NULL as [ROOT!1],
CategoryID as [categories!2!CategoryID],
CategoryName as [categories!2!CategoryName]
FROM categories
UNION ALL
select
2,
1,
NULL,
CategoryID,
CategoryName
From Categories
FOR XML EXPLICIT
输出结果:
<ROOT>
<categories CategoryID="1" CategoryName="Beverages"/>
<categories CategoryID="2" CategoryName="Condiments"/>
<categories CategoryID="3" CategoryName="Confections"/>
<categories CategoryID="4" CategoryName="Dairy Products"/>
<categories CategoryID="5" CategoryName="Grains/Cereals"/>
<categories CategoryID="6" CategoryName="Meat/Poultry"/>
<categories CategoryID="7" CategoryName="Produce"/>
<categories CategoryID="8" CategoryName="Seafood"/>
</ROOT>