sql server 中的xml

Sql server 提供的xml支持:

支持多种从普通列中选取数据并以xml格式接收数据的方法

支持在sql server中使用xml数据类型本地存储xml 数据

支持使用xquery和其他方法查询以原xml格式保存的数据

支持使用xml架构对以xml格式存储的数据强制数据完整性

支持对xml数据进行索引

支持具有层次结构的数据

 

可以使用内置的xml_schema_namespace()函数来查看现有的架构集合:

xml_schema_namespace(<sql server schema>,<xml schema collection>,[<namespace>])

 

创建,修改,删除xml架构集合:

Create xml schema collection [<sql server schema>.]<collection name>

As {<schema text>|<variable containing the schema text>}

 

Alter xml schema collection [<sql server schema>.]<collection name>

Add {<schema text>|<variable containing>}

 

Drop xml schema collection [<sql server schema>.]<collection name>

 

Xml数据类型方法

<instance of xml data type>.<method>

一共有以下5种可用方法:

Query:允许通过运行xquery格式的查询来访问xml,允许返回多个数据片段而非离散值

Value:允许访问特定元素或属性中的一个离散值

Modify:这是ms对xquery的扩展,可以对数据进行修改

Nodes:用于将xml数据拆分为单独的,关系型行

Exist:测试是否有某个特定的节点或属性在所测试的xml实例中

 

With xmlnamespaces(‘http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/productmodelmanuinstructions’as pi)

Select productmodelid,instructions.query(‘/pi:root/pi:location/pi:step’)as steps

From production.productmodel

Where productionmodelid=66;

 

instructions.value(‘(/pi:root/pi:location/@laborhours)[1]’,decimal(5,2))as location

 

set instructions.modify(‘replace value of(/pi:root/pi:location/@laborhours)[1] with 1.75’)

 

select pm.productmodelid,

            pmi.location.value(‘./@locationid’,’int’)as locationid,

from production.productmodel pm

cross apply pm.instructions.nodes(‘/pi:root/pi:location’)as pmi(location);

 

instructions.exist(‘/pi:root/pi:location/pi:step/pi:specs’)=1

 

约束不能使用xml数据类型方法,但可以使用函数

for xml 语句提供3种对结果进行xml格式化的初始选项

raw

auto

path

 

select  customerid, count(*) as ‘customerid/@ordercount’

from sales.salesorderheader orders

where customerid=29890 or customerid=30067

group by customerid

for xml path

openxml() 函数

 

 

 

 

转载于:https://www.cnblogs.com/ongoing/archive/2013/03/07/2947794.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值