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>
相关推荐
[ FOR { BROWSE | XML { RAW | AUTO | EXPLICIT } [ , XMLDATA ] [ , ELEMENTS ] [ , BINARY BASE64 ] } ] 参数 BROWSE 指定当查看 DB-Library 浏览模式游标中的数据时允许更新。如果表包含时间戳列(用 ...
EXPLICIT模式在Transact-SQL中检索XML数据的应用.pdf
主要介绍了MySQL 5.6 中的 TIMESTAMP 和 explicit_defaults_for_timestamp 参数,需要的朋友可以参考下
Fortran code for euler explicit
2018-xDeepFM- Combining Explicit and Implicit Feature Interactions for Recommender Systems
3.5 Downloading SQL Statements from the Web Site . . . . . . . . . . 38 3.6 Ready? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 CHAPTER 4 SQL in a Nutshell ...
Implicit-explicit Finite-difference Lattice Boltzmann Method for Compressible Flows,王勇,何雅玲,We propose an implicit-explicit finite-difference lattice Boltzmann method for compressible flows in ...
Best Practices Analyzer Tool for Microsoft SQL Server 2000是Microsoft SQL Server开发团队开发的一个数据库管理工具,可以让你检测设计的数据库是否遵循SQL Server操作和管理的最佳实践准则。这些准则公认有助于...
C++ explicit关键字在刚学的时候就接触到了,也从各处了解了一些,但始终云里雾里,在闲来无事的时候再仔细研究了一下,自己消化理解了一下,自认为比其他网友总结的精简而不失准确。
You may still update the column with an explicit UPDATE statement referencing the SQL UNISTR function. - Bug 4924518: Only the SQL Worksheet statement input area (code editor) uses a configurable ...
vumat subroutin for abaqus explicit
ANSYS Explicit Dynamics Analysis Guide
ABAQUS_Explicit_终极完美版
In this paper, we mainly consider finding an explicit formula for the inverse of a pentadiagonal Toeplitz matrix. For that purpose, we first factorize the modified form of a pentadiagonal Toeplitz ...
LINQ to SQL语句(23)之动态查询 90 1.Select 91 2.Where 92 LINQ to SQL语句(24)之视图 94 LINQ to SQL语句(25)之继承 96 1.一般形式 97 2.OfType形式 98 3.IS形式 98 4.AS形式 99 5.Cast形式 99 6.UseAsDefault形式...
Option Explicit Public txtSQL As String Dim mrc As adodb.Recordset '用户的读写权限标识 Dim mintRW As Integer '状态条中显示的时间信息 Public msBarText As String Public reportSQL As...
explicit vumat code material
C++入门学习——explicit关键字的作用,相关教程链接如下:http://blog.csdn.net/tennysonsky/article/details/49096977
Kotlin多平台库提供显式类型(Kotlin multiplatform library providing explicit types.,) 说明:Kotlin多平台库提供显式类型。, (Kotlin multiplatform library providing explicit types.,) 文件列表: ....