sample 1:

declare @x xml

select @x='<ArrayOfScheduledTime>
<ScheduledTime>
<RecurrenceType>EveryMonday</RecurrenceType>
<Stamp>
<dateTime>2000-01-01T19:30:00</dateTime>
</Stamp>
</ScheduledTime>
<ScheduledTime>
<RecurrenceType>EveryThursday</RecurrenceType>
<Stamp>
<dateTime>2000-01-01T19:30:00</dateTime>
</Stamp>
</ScheduledTime>
<ScheduledTime>
<RecurrenceType>EverySunday</RecurrenceType>
<Stamp>
<dateTime>2000-01-01T19:30:00</dateTime>
</Stamp>
</ScheduledTime>
</ArrayOfScheduledTime>' SELECT N.v.value('.' , 'VARCHAR(100)')B
FROM @x.nodes('/ArrayOfScheduledTime/ScheduledTime/RecurrenceType')N(v) /*
EveryMonday
EveryThursday
EverySunday
*/
select @x.query('/ArrayOfScheduledTime/ScheduledTime/RecurrenceType')
/*
<RecurrenceType>EveryMonday</RecurrenceType>
<RecurrenceType>EveryThursday</RecurrenceType>
<RecurrenceType>EverySunday</RecurrenceType>
*/
select o.value('RecurrenceType[1]','varchar(20)') 'RecurrenceType'
from (select @x 'x') t
cross apply x.nodes('/ArrayOfScheduledTime/ScheduledTime') x(o) /*
RecurrenceType
--------------------
EveryMonday
EveryThursday
EverySunday (3 行受影响)
*/
DECLARE @handel int;
EXEC sp_xml_preparedocument @handel output, @x
SELECT * from OPENXML(@handel,'/ArrayOfScheduledTime/ScheduledTime',2)
WITH(RecurrenceType varchar(50)) EXEC sp_xml_removedocument @handel /**************结果*****************
EveryMonday
EveryThursday
EverySunday
*/

sample2:

DECLARE @xDailyConfig XML
set @xDailyConfig='<ArrayOfScheduledTime>
<ScheduledTime>
<RecurrenceType>Everyday</RecurrenceType>
<Stamp>
<dateTime>2000-01-01T09:00:00</dateTime>
<dateTime>2000-01-01T13:00:00</dateTime>
<dateTime>2000-01-01T19:00:00</dateTime>
</Stamp>
</ScheduledTime>
</ArrayOfScheduledTime>' SELECT N.v.value('.' , 'VARCHAR(100)')B
FROM @xDailyConfig.nodes('/ArrayOfScheduledTime/ScheduledTime/Stamp/dateTime')N(v) /*
2000-01-01T09:00:00
2000-01-01T13:00:00
2000-01-01T19:00:00
*/
DECLARE @handel int;
EXEC sp_xml_preparedocument @handel output, @xDailyConfig
SELECT * from OPENXML(@handel,'/ArrayOfScheduledTime/ScheduledTime/Stamp/dateTime',3)
WITH(dateTime varchar(50) '.[1]') EXEC sp_xml_removedocument @handel /*
2000-01-01T09:00:00
2000-01-01T13:00:00
2000-01-01T19:00:00
*/

--  统计每一个RecurrenceType对其相应的dateTime的个数

declare @x xml

 select @x='<ArrayOfScheduledTime>
<ScheduledTime>
<RecurrenceType>EverySaturday</RecurrenceType>
<Stamp>
<dateTime>2000-01-01T07:00:00</dateTime>
<dateTime>2000-01-01T08:00:00</dateTime>
</Stamp>
</ScheduledTime>
<ScheduledTime>
<RecurrenceType>EveryWednesday</RecurrenceType>
<Stamp>
<dateTime>2000-01-01T09:00:00</dateTime>
</Stamp>
</ScheduledTime>
<ScheduledTime>
<RecurrenceType>EveryFriday</RecurrenceType>
<Stamp>
<dateTime>2000-01-01T09:00:00</dateTime>
<dateTime>2000-01-01T09:20:00</dateTime>
<dateTime>2000-01-01T09:40:00</dateTime>
</Stamp>
</ScheduledTime>
<ScheduledTime>
<RecurrenceType>EverySunday</RecurrenceType>
<Stamp>
<dateTime>2000-01-01T09:00:00</dateTime>
</Stamp>
</ScheduledTime>
</ArrayOfScheduledTime>' SELECT T2.RecurrenceType,COUNT(T3.[dateTime]) [Count]
FROM
( SELECT CONVERT(XML,N.v.query('.'))C1
FROM @x.nodes('/ArrayOfScheduledTime/ScheduledTime')N(v))T1
OUTER APPLY(
SELECT M.v.value('.','VARCHAR(100)')RecurrenceType
FROM T1.C1.nodes('//RecurrenceType') M(v)
)T2
OUTER APPLY(
SELECT L.v.value('.','VARCHAR(100)')[dateTime]
FROM T1.C1.nodes('//dateTime') L(v)
)T3
GROUP BY T2.RecurrenceType
/*
RecurrenceType Count
-------------------- -----------
EverySaturday 2
EveryWednesday 1
EveryFriday 3
EverySunday 1
*/ SELECT N.v.query('RecurrenceType').value('.','VARCHAR(20)') RecurrenceType,
N.v.query('count(Stamp//dateTime)').value('.','int') [Count]
FROM @x.nodes('/ArrayOfScheduledTime/ScheduledTime')N(v)
/*
RecurrenceType Count
-------------------- -----------
EverySaturday 2
EveryWednesday 1
EveryFriday 3
EverySunday 1
*/

其他例子:

       declare @x xml=
'<ArrayOfGuid>
<guid>754350a3-228e-4981-a430-a5f62af9b936</guid>
<guid>792f9404-6330-4302-999a-3ec36e9e1275</guid>
</ArrayOfGuid>' -- get count
SELECT TOP 1 N.v.query('count(//guid)').value('.','varchar(100)') [COUNT]
FROM @x.nodes('/ArrayOfGuid/guid')N(v) -- get each guidValue
SELECT N.v.value('.','VARCHAR(100)') guidValue
--N.v.query('count(//guid)').value('.','VARCHAR(100)') [Count]
FROM @x.nodes('/ArrayOfGuid/guid')N(v) /* result COUNT
----------------------------------------------------------------------------------------------------
2 (1 行受影响) guidValue
----------------------------------------------------------------------------------------------------
754350a3-228e-4981-a430-a5f62af9b936
792f9404-6330-4302-999a-3ec36e9e1275 (2 行受影响)
*/

other sample 2:

  declare @x xml
='<Audits OperatorKey="77de120a-7704-49b1-8980-8f1e9ad65edd">
<AuditItem SourceName="PatientProfile" SourceIdentity="77de120a-7704-49b1-8980-8f1e9ad65edd">
<DataXml>
<PatientProfile>
<Key p2:nil="true" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" />
<CreatedStamp>2015-07-28T03:18:46.843Z</CreatedStamp>
<LastUpdatedStamp>2015-07-28T03:18:46.844Z</LastUpdatedStamp>
<State>Normal</State>
<UserKey>77de120a-7704-49b1-8980-8f1e9ad65edd</UserKey>
<FirstName>zhang</FirstName>
<LastName>andy</LastName>
<MiddleName />
<Gender>Male</Gender>
<Birthday p2:nil="true" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" />
<AvatarCode />
<AvatarKey p2:nil="true" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" />
<ProgramKeyList />
<Overall />
<BirthLocation />
<Residence />
<BodyHeight>0</BodyHeight>
<MainDisease />
<OtherDisease>
<TimeBasedDiseaseItemOfDiseaseName>
<Time>1</Time>
<DiseaseItem>
<Key>15aabe2b-73af-426d-b668-b93fb84035ec</Key>
<CreatedStamp>2015-07-28T03:41:01.1260093Z</CreatedStamp>
<LastUpdatedStamp>2015-07-28T03:41:01.1260093Z</LastUpdatedStamp>
<State>Normal</State>
<ThirdPartyIdentity>542cb968-f806-46ee-9e9d-8ba5bdbf8da7</ThirdPartyIdentity>
<IsMain>false</IsMain>
<Name />
<CultureInfo>zh-cn</CultureInfo>
</DiseaseItem>
</TimeBasedDiseaseItemOfDiseaseName>
</OtherDisease>
<Symptoms />
<MedicalHistory />
<Infections />
<Allergies />
<FamilyHistory />
<SmokingHistory />
<DrinkingHistory />
<EmergencyContact />
<SurgeryHistory>
<SurgeryHistory>
<Key p4:nil="true" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance" />
<CreatedStamp>2015-07-28T03:18:46.848Z</CreatedStamp>
<LastUpdatedStamp>2015-07-28T03:18:46.849Z</LastUpdatedStamp>
<State>Normal</State>
<SurgeryDate>2015-02-01T03:18:46.849Z</SurgeryDate>
<Surgery>
<Key>b259b887-ac76-4f01-a03d-afa1a8e238a2</Key>
<CreatedStamp>2015-07-28T03:18:41.685Z</CreatedStamp>
<LastUpdatedStamp>2015-07-28T03:18:41.688Z</LastUpdatedStamp>
<State>Normal</State>
<NameCN>肝移植</NameCN>
<NameEN>Liver Transplant</NameEN>
</Surgery>
</SurgeryHistory>
</SurgeryHistory>
<PatientLocation>
<Key p3:nil="true" xmlns:p3="http://www.w3.org/2001/XMLSchema-instance" />
<CreatedStamp>2015-07-28T03:18:46.845Z</CreatedStamp>
<LastUpdatedStamp>2015-07-28T03:18:46.847Z</LastUpdatedStamp>
<State>Normal</State>
<UserKey>77de120a-7704-49b1-8980-8f1e9ad65edd</UserKey>
<GeographyTierKey_Province>d4538110-24fc-4edd-9320-1f6b62b192fa</GeographyTierKey_Province>
<GeographyTierKey_City>a92c824c-512e-4d8b-812d-448b95546662</GeographyTierKey_City>
<Province>吉林省</Province>
<City>白城市</City>
</PatientLocation>
<DrugRemind>
<DrugRemind>
<Key>db9808b9-4957-47c6-99c1-92ca97548392</Key>
<CreatedStamp>2015-07-28T03:13:18.753Z</CreatedStamp>
<LastUpdatedStamp>2015-07-28T03:13:18.753Z</LastUpdatedStamp>
<State>Normal</State>
<TargetUserKey>77de120a-7704-49b1-8980-8f1e9ad65edd</TargetUserKey>
<DrugKey>00000000-0000-0000-0000-000000000000</DrugKey>
<BrandKey>00000000-0000-0000-0000-000000000000</BrandKey>
<DrugName>新山地明/普乐可复</DrugName>
<BrandName>新山地明/普乐可复</BrandName>
<BoxSize>0</BoxSize>
<Unit>;2-6 mg</Unit>
<Amount>1</Amount>
<DoseStrength>0</DoseStrength>
<DoseStrengthWithUnit>2-6 mg</DoseStrengthWithUnit>
<DoseTimePin>
<Recurrence>EveryHour</Recurrence>
<ReferenceStamps>
<dateTime>2000-01-01T00:00:00Z</dateTime>
<dateTime>2000-01-01T12:00:00Z</dateTime>
</ReferenceStamps>
<StartStamp>2014-08-22T00:00:00Z</StartStamp>
<EndStamp p5:nil="true" xmlns:p5="http://www.w3.org/2001/XMLSchema-instance" />
</DoseTimePin>
<CreatedBy>77de120a-7704-49b1-8980-8f1e9ad65edd</CreatedBy>
</DrugRemind>
<DrugRemind>
<Key>bb49ed53-b16f-4caa-882e-82dcdf8ce991</Key>
<CreatedStamp>2015-07-28T03:13:18.747Z</CreatedStamp>
<LastUpdatedStamp>2015-07-28T03:13:18.747Z</LastUpdatedStamp>
<State>Normal</State>
<TargetUserKey>77de120a-7704-49b1-8980-8f1e9ad65edd</TargetUserKey>
<DrugKey>00000000-0000-0000-0000-000000000000</DrugKey>
<BrandKey>00000000-0000-0000-0000-000000000000</BrandKey>
<DrugName>米芙/骁悉</DrugName>
<BrandName>米芙/骁悉</BrandName>
<BoxSize>0</BoxSize>
<Unit>;1080 mg</Unit>
<Amount>1</Amount>
<DoseStrength>0</DoseStrength>
<DoseStrengthWithUnit>1080 mg</DoseStrengthWithUnit>
<DoseTimePin>
<Recurrence>EveryHour</Recurrence>
<ReferenceStamps>
<dateTime>2000-01-01T00:00:00Z</dateTime>
<dateTime>2000-01-01T12:00:00Z</dateTime>
</ReferenceStamps>
<StartStamp>2014-08-22T00:00:00Z</StartStamp>
<EndStamp p5:nil="true" xmlns:p5="http://www.w3.org/2001/XMLSchema-instance" />
</DoseTimePin>
<CreatedBy>77de120a-7704-49b1-8980-8f1e9ad65edd</CreatedBy>
</DrugRemind>
</DrugRemind>
</PatientProfile>
</DataXml>
</AuditItem>
</Audits>' --declare @GeographyTierKey_Province varchar(100)
-- select @GeographyTierKey_Province=@x.value('(Audits/AuditItem/DataXml/PatientProfile/PatientLocation/GeographyTierKey_Province)[0]','varchar(100)') -- print @GeographyTierKey_Province declare @GeographyTierKey_Province varchar(100)
--select @GeographyTierKey_Province=@x.value('(Audits/AuditItem/DataXml/PatientProfile/PatientLocation/GeographyTierKey_Province)[0]','varchar(100)') --print @GeographyTierKey_Province
select @GeographyTierKey_Province=(SELECT top 1 N.v.value('.','VARCHAR(100)') guidValue
--N.v.query('count(//guid)').value('.','VARCHAR(100)') [Count]
FROM @x.nodes('Audits/AuditItem/DataXml/PatientProfile/PatientLocation/GeographyTierKey_Province')N(v) ) select @GeographyTierKey_Province
-- d4538110-24fc-4edd-9320-1f6b62b192fa

来源:

SQL特殊语句的笔记

http://www.2cto.com/database/201205/133329.html

说明,使用 openxml后。一定要记得用 sp_xml_removedocument 释放xml document资源。

(结束)

sql for xml query sample的更多相关文章

  1. 转载---SQL Server XML基础学习之<5>--XQuery(query)

    本章写一些SQL Server XML的一些XQuery基础语法,主要讲的query查询语法 T-SQL 支持用于查询 XML 数据类型的 XQuery 语言的子集. XQuery 基于现有的 XPa ...

  2. SQL SERVER XML 学习总结

    SQL  SERVER  XML  学习总结 最新的项目任务要做一个数据同步的功能,这些天都在做技术准备,主要是用到了微软的Service Broker技术,在熟悉使用该技术的同时,又用到了Sql s ...

  3. 转载---SQL Server XML基础学习之<6>--XQuery的 value() 方法、 exist() 方法 和 nodes() 方法

    /*------------------------------------------------------------------------------+ #| = : = : = : = : ...

  4. SQL Server XML数据解析

    --5.读取XML --下面为多种方法从XML中读取EMAIL DECLARE @x XML SELECT @x = ' <People> <dongsheng> <In ...

  5. SQL Server XML 查询

    [参考1] 18个小实例入门SQLServer XML查询 [参考2] 转载---SQL Server XML基础学习之<5>--XQuery(query)

  6. Sql Server xml 类型字段的增删改查

    1.定义表结构 在MSSM中新建数据库表CommunicateItem,定义其中一个字段ItemContentXml 为xml类型 2.编辑表数据,新增一行,发现xml类型不能通过设计器录入数据. 需 ...

  7. 解决java.sql.SQLException: ORA-01789: query block has incorrect number of result columns

    java.sql.SQLException: ORA-01789: query block has incorrect number of result columns at oracle.jdbc. ...

  8. sql分组合并字段重复项sql for xml path

    -------------------------(情景描述) 在我们处理数据时,可能会碰到这种情景: Id                Name 1                  a,b 2  ...

  9. Oracle使用Sql把XML解析成表(Table)的方法

    SELECT * FROM XMLTABLE('$B/DEAL_BASIC/USER_DEAL_INFO' PASSING XMLTYPE('<?xml version="1.0&qu ...

随机推荐

  1. [Java开发之路](9)对象序列化与反序列化

    1. 对象序列化 当你创建对象时.仅仅要你须要.它会一直存在,可是程序终止时,不管何时它都不会继续存在.虽然这样做是很有意义的,可是在某些情况下.假设程序不执行时扔能存在而且保存其信息,那将对我们很实 ...

  2. PopupMenu-使用实例跟监听事件

    今天需要给一个控件添加弹出菜单功能.就顺便学习了下popupMenu的使用,记录下来. 它的使用其实也非常的简单,看如下代码 popupMenu = new PopupMenu(MainActivit ...

  3. 31.Intellij idea 的maven项目如何通过maven自动下载jar包

    转自:https://blog.csdn.net/u012851114/article/details/81872981 maven项目自动加载jar包 所需工具如下: Intellij IDEA 1 ...

  4. Vue项目自动转换 px 为 rem,高保真还原设计图

    技术栈 vue-cli:使用脚手架工具创建项目. postcss-pxtorem:转换px为rem的插件. 自动设置根节点html的font-size 因为rem单位是相对于根节点的字体大小的,所以通 ...

  5. Js将类数组转化为数组

    说起伪数组,大家可能会想到arguments, 这个我们函数参数的一个类数组,是类数组的代表. 1.拥有length属性,可以使用下标来访问元素,这两点和数组相同. 2.不能使用数组的方法,他们不能使 ...

  6. 玲珑学院 1014 Absolute Defeat

    SAMPLE INPUT 3 2 2 2 1 1 5 1 4 1 2 3 4 5 4 10 3 1 2 3 4 SAMPLE OUTPUT 1 0 15 前缀和,每个元素都判断一下. #include ...

  7. mysql 编码错误修改

    set character_set_results=utf8;

  8. (九)unity4.6学习Ugui中文文档-------參考-UGUI Rect Transform

     大家好.我是孙广东.   转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:http://www.unit ...

  9. Autodesk 招聘Revit二次开发咨询顾问,与Autodesk全球团队紧密合作,提高职业生涯的好机会

    朋友们, 因为我离开Autodesk的全职工作(变为部分时间工作),我的职位空出.急招这个职位.请踊跃把你周围的朋友推荐给Autodesk. 请将简历发给我转交给Autodesk 我的邮箱yexion ...

  10. Google、Mozilla、Qt、LLVM 这几家的规范是明确禁用异常的

    作者:陈硕链接:https://www.zhihu.com/question/22889420/answer/22975569来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...