>  기사  >  데이터 베이스  >  SQLSERVER中XML查询:FORXML指定RAW

SQLSERVER中XML查询:FORXML指定RAW

WBOY
WBOY원래의
2016-06-07 16:20:461043검색

SQL SERVER中XML查询:FOR XML指定RAW前言 在SQL SERVER中,XML查询可以指定RAW,AUTO,EXPLICIT,PATH。本文用一些实例介绍SQL SERVER中指定RAW的XML查询。 基础FOR XML查询 看实例: with TestXml as ( select 1 as id,'LeeWhoeeUniversity' as name union

SQL SERVER中XML查询:FOR XML指定RAW 前言

在SQL SERVER中,XML查询可以指定RAW,AUTO,EXPLICIT,PATH。本文用一些实例介绍SQL SERVER中指定RAW的XML查询。

基础FOR XML查询

看实例:

with TestXml
as
(
select 1 as id,'LeeWhoeeUniversity' as name
union all
select 2,'SQLSERVER中XML查询'
union all
select 3 ,'FOR XML'
)
select id,name from testxml for xml raw,type

运行后结果:



红色字体type可选,不会影响结果,只是影响数据类型。

指定 ELEMENTS:

with TestXml
as
(
select 1 as id,N'LeeWhoeeUniversity' as name
union all
select 2,N'SQLSERVER中XML查询'
union all
select 3 ,null
)
select id,name from testxml for xml raw,elements

注意,第三行值改为NULL值进行测试。

结果:


1
LeeWhoeeUniversity


2
SQLSERVER中XML查询


3

元素name在第三行没有出现,,因为是NULL值。

但是我们可以用XSINIL生成NULL值的name元素。

如:

with TestXml
as
(
select 1 as id,N'LeeWhoeeUniversity' as name
union all
select 2,N'SQLSERVER中XML查询'
union all
select 3 ,null
)
select id,name from testxml for xml raw,elements XSINIL
运行结果:


1
LeeWhoeeUniversity


2
SQLSERVER中XML查询


3

使用XMLDATA和XMLSCHEMA

XMLDATA返回描述文档结构的 XML-DATA 架构。

如:

with TestXml
as
(
select 1 as id,N'LeeWhoeeUniversity' as name
union all
select 2,N'SQLSERVER中XML查询'
union all
select 3 ,null
)
select id,name from testxml for xml raw,XMLDATA


结果:











XML SCHEMA

通过指定 XMLSCHEMA 选项,您可以针对结果请求 XSD 架构:

with TestXml
as
(
select 1 as id,N'LeeWhoeeUniversity' as name
union all
select 2,N'SQLSERVER中XML查询'
union all
select 3 ,null
)
select id,name from testxml for xml raw,XMLSCHEMA
结果:


















您可以将目标命名空间 URI 指定为 FOR XML 中 XMLSCHEMA 的可选参数。

with TestXml
as
(
select 1 as id,N'LeeWhoeeUniversity' as name
union all
select 2,N'SQLSERVER中XML查询'
union all
select 3 ,null
)
select id,name from testxml for xml raw,XMLSCHEMA ('urn:')

结果:

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.