XML Data Type Methods(一)
/*XML Data Type Methods:
1.The query('XQuery') method retrieves(vt.检索,重新得到) a subset of untyped XML from the target XML instance 2.The value('XQuery',dataType) method returns a scalar value(标量值) from the targeted XML document.
The returned value is converted to the data type you specify when you call the method. 3.The exist('XQuery') method lets you test for the existence of an element or one of its values 4.The nodes('XQuery') method returns what is essentially a table that includes one column.
That means you should use the method only in those parts of a statement that can handle rowset views, such as the FROM clause.
It also means that, when you call the nodes() method, you must assign a table alias and column alias to the rowset view returned by the method
5.The modify('XQuery') method lets you update that data
*/ DECLARE @StoresTable TABLE
(
StoreID INT IDENTITY(1,1) PRIMARY KEY,
Survey_untyped XML,
Survey_typed XML
) INSERT INTO @StoresTable(Survey_untyped,Survey_typed)
VALUES
(
'<UnifiedRequest>
<CommonInfo>
<Version>1.14</Version>
<Username>EC</Username>
</CommonInfo>
<OrderInfo>
<CompanyCode>1003</CompanyCode>
<PayTermsCode>024</PayTermsCode>
</OrderInfo>
</UnifiedRequest>',
'<UnifiedRequest
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/UnifiedRequest.xsd">
<CommonInfo>
<Version>1.14</Version>
<Username>EC</Username>
</CommonInfo>
<OrderInfo>
<CompanyCode>1003</CompanyCode>
<PayTermsCode>024</PayTermsCode>
</OrderInfo>
</UnifiedRequest>'
) --1.UntypedColumn
--1.1 query(xpathParameter) method
SELECT
Survey_untyped.query('/UnifiedRequest/OrderInfo') AS Info_typed
FROM
@StoresTable
/*Result:
<OrderInfo>
<CompanyCode>1003</CompanyCode>
<PayTermsCode>024</PayTermsCode>
</OrderInfo>
*/
--1.2 value(xpathParameter) method
SELECT
Survey_untyped.value('(/UnifiedRequest/OrderInfo/CompanyCode/text())[1]','INT') AS CompanyCode
FROM
@StoresTable --1.3 exist(xpathParameter) method
SELECT TOP(1)
CASE
WHEN Survey_untyped.exist('/UnifiedRequest/OrderInfo/CompanyCode/text()')=1 THEN 'Found'
ELSE 'Not Found'
END
FROM
@StoresTable --1.4 nodes(xpathParameter) method
DECLARE @bikes XML
SET @bikes =
'<Products>
<Product>Mountain</Product>
<Product>Road</Product>
</Products>'
SELECT
Category.query('./text()') AS BikeTypes1,--return a subset of xml
Category.value('(./text())[1]','VARCHAR(20)') AS BikeTypes2--return string
FROM
@bikes.nodes('/Products/Product')
AS Bike(Category);
/*Result
BikeTypes1 BikeTypes2
---------------------------
Mountain Mountain
Road Road
*/ --2.TypedColumn which contains namespace
--2.1 query(xpathParameter) method
;WITH XMLNAMESPACES(DEFAULT 'http://tempuri.org/UnifiedRequest.xsd')
SELECT
Survey_typed.query('/UnifiedRequest/OrderInfo') AS Info_typed
FROM
@StoresTable
/*Result:
<p1:OrderInfo xmlns:p1="http://tempuri.org/UnifiedRequest.xsd">
<p1:CompanyCode>1003</p1:CompanyCode>
<p1:PayTermsCode>024</p1:PayTermsCode>
</p1:OrderInfo>
*/ ;WITH XMLNAMESPACES('http://tempuri.org/UnifiedRequest.xsd' AS UFD)
SELECT
Survey_typed.query('/UFD:UnifiedRequest/UFD:OrderInfo') AS Info_typed
FROM
@StoresTable
/*Result:
<UFD:OrderInfo xmlns:UFD="http://tempuri.org/UnifiedRequest.xsd">
<UFD:CompanyCode>1003</UFD:CompanyCode>
<UFD:PayTermsCode>024</UFD:PayTermsCode>
</UFD:OrderInfo>
*/ SELECT
Survey_typed.query('declare namespace UFR="http://tempuri.org/UnifiedRequest.xsd";
/UFR:UnifiedRequest/UFR:OrderInfo') AS Info_typed
FROM
@StoresTable;
/*Result:
<UFR:OrderInfo xmlns:UFR="http://tempuri.org/UnifiedRequest.xsd">
<UFR:CompanyCode>1003</UFR:CompanyCode>
<UFR:PayTermsCode>024</UFR:PayTermsCode>
</UFR:OrderInfo>
*/ ;WITH XMLNAMESPACES('http://tempuri.org/UnifiedRequest.xsd' AS UFD)
SELECT
Survey_typed.query('/UFD:UnifiedRequest') AS Info_typed
FROM
@StoresTable;
/*Result:
<UnifiedRequest xmlns="http://tempuri.org/UnifiedRequest.xsd" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<CommonInfo>
<Version>1.14</Version>
<Username>EC</Username>
</CommonInfo>
<OrderInfo>
<CompanyCode>1003</CompanyCode>
<PayTermsCode>024</PayTermsCode>
</OrderInfo>
</UnifiedRequest>
*/ --2.2 value(xpathParameter,dataType) method
;WITH XMLNAMESPACES(DEFAULT 'http://tempuri.org/UnifiedRequest.xsd')
SELECT
--Result:1003
Survey_typed.value('(/UnifiedRequest/OrderInfo/CompanyCode/text())[1]','INT') AS CompanyCode
FROM
@StoresTable SELECT
--Result:1003
Survey_typed.value('declare namespace UFD="http://tempuri.org/UnifiedRequest.xsd";
(/UFD:UnifiedRequest/UFD:OrderInfo/UFD:CompanyCode/text())[1]','INT') AS CompanyCode
FROM
@StoresTable --2.3 exist(xpathParameter) method
;WITH XMLNAMESPACES(DEFAULT 'http://tempuri.org/UnifiedRequest.xsd')
SELECT TOP(1)
CASE
WHEN Survey_typed.exist('/UnifiedRequest/OrderInfo/CompanyCode/text()')=1 THEN 'Found'
ELSE 'Not Found'
END FROM
@StoresTable --2.4 nodes(xpathParameter) method
DECLARE @bikes2 XML
SET @bikes2 =
'<Products xmlns="http://tempuri.org/UnifiedRequest.xsd">
<Product>Mountain</Product>
<Product>Road</Product>
</Products>' ;WITH XMLNAMESPACES(DEFAULT 'http://tempuri.org/UnifiedRequest.xsd')
SELECT
Category.query('./text()') AS BikeTypes1,--return a subset of xml
Category.value('(./text())[1]','VARCHAR(20)') AS BikeTypes2--return string
FROM
@bikes2.nodes('/Products/Product') AS Bike(Category);
/*Result
BikeTypes1 BikeTypes2
---------------------------
Mountain Mountain
Road Road
*/

XML Data Type Methods(一)的更多相关文章

  1. xml Data Type Methods in sql server

    nodes() Method (xml Data Type) https://docs.microsoft.com/en-us/sql/t-sql/xml/nodes-method-xml-data- ...

  2. SQL Server error "Xml data type is not supported in distributed queries" and workaround for it

    Recently while working with data migration,got an error while running a following query where Server ...

  3. no data type for node

    java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.IdentNode  \-[IDE ...

  4. 关于使用FusionCharts生成图表时出现invalid xml data错误提示的解决方法

    FusionCharts的确功能是够强大的.收集的功能估计更强大.在初次使用时,对着手册,一步一步操作,就是生成图表工具不成功.一直报"Invalid xml data"错误.后面 ...

  5. PHP 笔记一(systax/variables/echo/print/Data Type)

    PHP stands for "Hypertext Preprocessor" ,it is a server scripting language. What Can PHP D ...

  6. JAVA 1.2(原生数据类型 Primitive Data Type)

    1. Java的数据类型分为2类 >> 原生数据类型(primitive data type) >> 引用数据类型(reference data type) 3. 常量和变量 ...

  7. salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解

    建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schema Builder查看表结构以及多表之间的关联关系,可以登录后点击setup在左侧搜索框输入schema ...

  8. The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

    刚刚有在程序中,传递一个空值至MS SQL Server数据库,这个值的数据类型为DATETIME执行时,它却发生了如标题提示的异常:The conversion of a varchar data ...

  9. WCF服务接口多,客户端在引用时出错!报WCF The maximum nametable character count quota (16384) has been exceeded while reading XML data错误

    WCF服务接口多,客户端在引用时出错!报WCF The maximum nametable character count quota (16384) has been exceeded while ...

随机推荐

  1. IOS 音效

    IOS 音效 音效我们也可以成为短音频通常在程序中播放时间为1~2秒. 在应用程序中起到点缀效果,提升整体用户体验 音效文件只需要加载一次 示例代码: // // ViewController.m / ...

  2. iOS 简单动画 序列帧动画

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ NSLog(@"旭宝爱吃 ...

  3. mysql regexp用法

    正则表达式作用是匹配方本,将一个模式(正则表达式)与一个文本串进行比较. MySQL用WHERE子句对正则表达式提供了初步的支持,允许你指定用正则表达式过滤SELECT检索出的数据. MySQL仅支持 ...

  4. 如何从SharePoint Content DB中查询List数据

    SharePoint用来维护基础数据非常方便,只需要建立自定义列表,然后使用InfoPath自定义一下维护界面,就可以实现在线的增删改查,开发效率很高.如果维护的数据需要进行审批,还可以加入工作流功能 ...

  5. dyld: Library not loaded: @rpath/libswiftCore.dylib 解决方法

    解决: 设置Build Setting - > 搜索 embe关键字 -> 修改属性 见如下图: 如果更新了Xcode 8 这里变成:

  6. Debian deb源方法升级PHP软件包

    学习Linxu以来,一直坚持编译方式安装软件包,貌似圈子里面也是都倾向于编译,可是搜索到的编译方法都是一堆复制粘贴来的指令, 每个人都这么编译, 却几乎没有人去写明那些五花八门的编译指令代表了什么,是 ...

  7. 配置nginx好了,html能打开,index.php打不开?

    启动这2个  #service nginx restart  # service php-fpm restart

  8. java 判断两个时间相差的天数

    1.实现目标 输入:两个日期 输出:两个日期相差的天数 2.代码实现 方法1: 通过Calendar类的日期比较.注意:这里需要考虑一下: 日期是跨年份的,如一个是2012年,一个是2015年的   ...

  9. Ubuntu14.04桌面版基本配置

    最近公司需要用Ubuntu测试一些功能,安装好后绚丽的桌面环境很是吸引人,但切换到字符界面后,发现不少地方与RHEL不一样,连ssh.vim都没有,字符界面玩得不开心. 搜了不少文章,才将基本的功能配 ...

  10. LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]

    题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...