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. 网络热恋之NSURLSession

    // // ViewController.m // NSURLSession代理简介 #import "ViewController.h" @interface ViewContr ...

  2. GHOST WIN7系统64位经典优化版 V2016年

    来自系统妈:http://www.xitongma.com 深度技术GHOST win7系统32,64位经典优化版 V2016年3月 系统概述 深度技术ghost win7系统64位经典优化版适用于笔 ...

  3. iOS开发 - 兼容iOS 10

    1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大改重构,这让开发者也体会到UserN ...

  4. 学习Entity Framework 中的Code First

    这是上周就写好的文章,是在公司浩哥的建议下写的,本来是部门里面分享求创新用的,这里贴出来分享给大家. 最近在对MVC的学习过程中,接触到了Code First这种新的设计模式,感觉很新颖,并且也体验到 ...

  5. linux 学习随笔-vim

    在自己的home/username目录下 更改vim的配置文件 如果没这个文件 copy其他人的配置文件 然后拖到此目录下 执行mv vimrc ~/.vimrc 更改名字 即可生效 只对当前用户生效 ...

  6. Jenkins用户配置(安装好jenkins后,怎么配置用户管理、权限管理)

    直奔主题 安装完成后,先开启用户配置 1. 系统管理-->配置权限 2.  启用安全,并选中"安全矩阵" 如上,搞定: 可以按用户去设置各项目的操作权限了: 轻松实现,jen ...

  7. MongoDB查询重复记录并保存到文件csv

    客户1w用户记录,发现里面有小部分重复数据 需要查出,比对哪些信息不同 https://docs.mongodb.org/manual/reference/operator/aggregation/# ...

  8. BIEE使用技巧

    索引: 1.如何清除缓存 2.通过“编辑 SQL”取得前一天的日期 3.格式化日历框参数 4.根据传入的开始时间和结束时间取得事实表中的指标(用到了3中的技巧) 5.直接调用数据库函数 6.时间格式转 ...

  9. Web API与国际化

    软件国际化是在软件设计和文档开发过程中,使得功能和代码设计能处理多种语言和文化习俗,在创建不同语言版本时,不需要重新设计源程序代码的软件工程方法.这在很多成熟的软件开发平台中非常常见.对于.net开发 ...

  10. mvn archetype:create和mvn archetype:generate

    create is deprecated in maven 3.0.5 and beyond,在maven3.0.5以上版本舍弃了create,使用generate生成项目 before:mvn ar ...