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/p/2947794.html