for xml 扩展功能
SQL Server 2005 通过 select 语句的 for xml 扩展功能,支持在服务器端以XML 文档的形式返回SQL 查询的结果。
语法如下:
select 表1.列名1, 表2.列名2 ...
from 表1,表2
for xml mode
其中,mode 指定使用:RAW、AUTO、EXPLICIT 或 PATH 模式。
(详细内容,查阅MSDN。)
创建示例环境:
创建表.sql
use
Winform
go
/* ***** 对象: Table [dbo].[Person] 脚本日期: 04/25/2010 12:56:29 ***** */
create table [ dbo ] . [ person ]
(
[ no ] [ int ] not null ,
[ name ] [ varchar ] ( 10 ) not null ,
[ sex ] [ varchar ] ( 2 ) not null ,
[ age ] [ int ] null ,
[ address ] [ varchar ] ( 50 ) null ,
constraint [ pk_table_1 ] primary key clustered
( [ no ] asc )
)
go
/* ***** 对象: Table [dbo].[grade] 脚本日期: 04/25/2010 12:59:24 ***** */
create table [ dbo ] . [ grade ]
(
[ gradeid ] [ int ] identity ( 1 , 1 ) not null ,
[ gradename ] [ varchar ] ( 20 ) null ,
[ remark ] [ varchar ] ( 20 ) null ,
[ img ] [ varbinary ] ( max ) null
)
go
/* ***** 对象: Table [dbo].[class] 脚本日期: 04/25/2010 13:00:28 ***** */
create table [ dbo ] . [ class ]
(
[ classid ] [ int ] identity ( 1 , 1 ) not null ,
[ gradeid ] [ int ] null ,
[ classname ] [ varchar ] ( 20 ) null ,
[ createdate ] [ datetime ] null ,
[ img ] [ varbinary ] ( max ) null ,
constraint [ pk_class ] primary key clustered
( [ classid ] asc )
)
go
go
/* ***** 对象: Table [dbo].[Person] 脚本日期: 04/25/2010 12:56:29 ***** */
create table [ dbo ] . [ person ]
(
[ no ] [ int ] not null ,
[ name ] [ varchar ] ( 10 ) not null ,
[ sex ] [ varchar ] ( 2 ) not null ,
[ age ] [ int ] null ,
[ address ] [ varchar ] ( 50 ) null ,
constraint [ pk_table_1 ] primary key clustered
( [ no ] asc )
)
go
/* ***** 对象: Table [dbo].[grade] 脚本日期: 04/25/2010 12:59:24 ***** */
create table [ dbo ] . [ grade ]
(
[ gradeid ] [ int ] identity ( 1 , 1 ) not null ,
[ gradename ] [ varchar ] ( 20 ) null ,
[ remark ] [ varchar ] ( 20 ) null ,
[ img ] [ varbinary ] ( max ) null
)
go
/* ***** 对象: Table [dbo].[class] 脚本日期: 04/25/2010 13:00:28 ***** */
create table [ dbo ] . [ class ]
(
[ classid ] [ int ] identity ( 1 , 1 ) not null ,
[ gradeid ] [ int ] null ,
[ classname ] [ varchar ] ( 20 ) null ,
[ createdate ] [ datetime ] null ,
[ img ] [ varbinary ] ( max ) null ,
constraint [ pk_class ] primary key clustered
( [ classid ] asc )
)
go
Demo 代码:
SQL 2005 For XML.sql
--
SQL 2005 For XML
use Winform
go
-- raw : 是以属性的方式显示字段
select * from Person for xml raw
-- 排序
select * from Person order by age for xml raw
-- auto : 是将表名(如:Person)作为元素名显示字段
select * from Person for xml auto
-- 两表联接: 使用auto后将以父子元素的形式显示xml
select classname,gradename,createdate
from class join grade
on grade.gradeid = class.gradeid for xml auto
-- 通过root属性添加根节点
select classname,gradename,createdate
from class join grade
on grade.gradeid = class.gradeid
for xml auto, root( ' Newer ' )
-- 通过elements属性将每个字段都以元素的形式显示
-- auto关键字将显示层次结构
select classname,gradename,createdate
from class join grade
on grade.gradeid = class.gradeid
for xml auto,root( ' Newer ' ),elements
-- 通过elements属性将每个字段都以元素的形式显示
-- row关键字将无层次结构
select classname,gradename,createdate
from class join grade
on grade.gradeid = class.gradeid
for xml raw ,root( ' Newer ' ),elements
-- 通过elements属性将每个字段都以元素的形式显示
-- path关键字将无层次结构(与row关键字一样),
-- 但无需elements关键字
select classname,gradename,createdate
from class join grade
on grade.gradeid = class.gradeid
for xml path,root( ' Newer ' )
use Winform
go
-- raw : 是以属性的方式显示字段
select * from Person for xml raw
-- 排序
select * from Person order by age for xml raw
-- auto : 是将表名(如:Person)作为元素名显示字段
select * from Person for xml auto
-- 两表联接: 使用auto后将以父子元素的形式显示xml
select classname,gradename,createdate
from class join grade
on grade.gradeid = class.gradeid for xml auto
-- 通过root属性添加根节点
select classname,gradename,createdate
from class join grade
on grade.gradeid = class.gradeid
for xml auto, root( ' Newer ' )
-- 通过elements属性将每个字段都以元素的形式显示
-- auto关键字将显示层次结构
select classname,gradename,createdate
from class join grade
on grade.gradeid = class.gradeid
for xml auto,root( ' Newer ' ),elements
-- 通过elements属性将每个字段都以元素的形式显示
-- row关键字将无层次结构
select classname,gradename,createdate
from class join grade
on grade.gradeid = class.gradeid
for xml raw ,root( ' Newer ' ),elements
-- 通过elements属性将每个字段都以元素的形式显示
-- path关键字将无层次结构(与row关键字一样),
-- 但无需elements关键字
select classname,gradename,createdate
from class join grade
on grade.gradeid = class.gradeid
for xml path,root( ' Newer ' )
本文转自钢钢博客园博客,原文链接:http://www.cnblogs.com/xugang/archive/2010/05/08/1730424.html,如需转载请自行联系原作者