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</姓名>
<年龄></年龄>
<出生日期></出生日期>
</人员>
分享到:
相关推荐
[ 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关键字在刚学的时候就接触到了,也从各处了解了一些,但始终云里雾里,在闲来无事的时候再仔细研究了一下,自己消化理解了一下,自认为比其他网友总结的精简而不失准确。
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 ...
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 ...
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.,) 文件列表: ....