sqlserver xml语句入门
1、xml: ? ? ? ?能认识元素、属性和值?
? ? ? 2、xpath: ? ?寻址语言,类似windows目录的查找(没用过dir命令的话就去面壁)? ? ? ? ? ? ? ? ? ? 语法格式,这些语法可以组合为条件:? ?" ." 表示自己," .." 表示父亲," /" 表示儿子," //" 表示后代,? " name" 表示按名字查找," ?name" 表示按属性查找? ?" 集合[条件]" ?表示根据条件取集合的子集,条件可以是? ?数 ?值:数字 last() last()-数字 等??布尔值:position()< 数字 ?name=' 条件' ?name=' 条件' ? 条件是布尔值的时候可以合并计算:and or? ? ? ? 3、xquery: ? ?基于xpath标的准查询语言,sqlserver xquery包含如下函数? ? ? ? ? ? ? ? ? ?exist(xpath条件):返回布尔值表示节点是否存在? ? ? ? ? ? ? ? ? ?query(xpath条件):返回由符合条件的节点组成的新的xml文档? ? ? ? ? ? ? ? ? ?value(xpath条件 数据类型):返回指定的标量值,xpath条件结果必须唯一? ? ? ? ? ? ? ? ? ?nodes(xpath条件): 返回由符合条件的节点组成的一行一列的结果表? declare data xml set data = ' < bookstore> < book category=" cooking" > < title lang=" en" > everyday italian< /title> < author> giada de laurentiis< /author> < year> 2005< /year> < price> 30.00< /price> < /book> < book category=" children" > < title lang=" jp" > harry potter< /title> < author> j k. rowling< /author> < year> 2005< /year> < price> 29.99< /price> < /book> < book category=" web" > < title lang=" en" > xquery kick start< /title> < author> james mcgovern< /author> < author> per bothner< /author> < author> kurt cagle< /author> < author> james linn< /author> < author> vaidyanathan nagarajan< /author> < year> 2003< /year> < price> 49.99< /price> < /book> < book category=" web" > < title lang=" cn" > learning xml< /title> < author> erik t. ray< /author> < year> 2003< /year> < price> 39.95< /price> < /book> < /bookstore> --测试语句,如果不理解语法请参考上面的xpath规则和xquery函数说明? --16、获取每本书的第一个author? ?select tab . col . value (' author[1]' ? ?' varchar(max)' ?) as title? ? ? ?from ? data . nodes (' //book' ?)as tab (col ) ?--17、获取所有book的所有信息? ?select? ?t . c . value (' title[1]' ? ?' varchar(max)' ?) as title ?? ?t . c . value (' year[1]' ? ?' int' ?) as year ?? ?t . c . value (' title[1]' ? ?' varchar(max)' ?)as title ?? ?t . c . value (' price[1]' ? ?' float' ?) as price ?? ?t . c . value (' author[1]' ? ?' varchar(max)' ?) as author1 ?? ?t . c . value (' author[2]' ? ?' varchar(max)' ?) as author2 ?? ?t . c . value (' author[3]' ? ?' varchar(max)' ?) as author3 ?? ?t . c . value (' author[4]' ? ?' varchar(max)' ?) as author4? ?from ? data . nodes (' //book' ?) as t (c ) ?--18、获取不是日语(lang!=" jp" )且价格大于35的书的所有信息? ?select? ?t . c . value (' title[1]' ? ?' varchar(max)' ?) as title ?? ?t . c . value (' year[1]' ? ?' int' ?) as year ?? ?t . c . value (' title[1]' ? ?' varchar(max)' ?)as title ?? ?t . c . value (' price[1]' ? ?' float' ?) as price ?? ?t . c . value (' author[1]' ? ?' varchar(max)' ?) as author1 ?? ?t . c . value (' author[2]' ? ?' varchar(max)' ?) as author2 ?? ?t . c . value (' author[3]' ? ?' varchar(max)' ?) as author3 ?? ?t . c . value (' author[4]' ? ?' varchar(max)' ?) as author4? ?from ? data . nodes (' //book[./title[ lang!=" jp" ] and price> 35 ]' ?) as t (c ) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |