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. SSH 框架

    SSH是 struts+spring+hibernate的一个集成框架,是目前较流行的一种web应用程序开源框架.是把多个框架(Struts.Spring以及Hibernate)紧密的结合在一起,用于 ...

  2. 安装php扩展

    下面我以soap安装为例子 cd /home/zhangy/php-5.2.6/ext/soap /usr/local/php/bin/phpize#确定php-config文件在不在,调用php-c ...

  3. js:插入节点appendChild insertBefore使用方法

    首先 从定义来理解 这两个方法: appendChild() 方法:可向节点的子节点列表的末尾添加新的子节点.语法:appendChild(newchild) insertBefore() 方法:可在 ...

  4. 写在复习MVC后

    MVC的一些 今天把MVC复习了下,包括官方文档以及各种中文博客. 官方文档里面最能说明的问题的图片,相对于传统的MVC,苹果分离了View和Model之间的通信,实现了更好的复用性.我觉得MVC更 ...

  5. XML 学习介绍 收藏

    XML学习总结(一)——XML介绍 一.XML概念 Extensible Markup Language,翻译过来为可扩展标记语言.Xml技术是w3c组织发布的,目前推荐遵循的是W3C组织于2000发 ...

  6. MYSQL管理之主从同步管理

    原文地址:MYSQL管理之主从同步管理 作者:飞鸿无痕 MYSQL管理之主从同步管理 MYSQL主从同步架构是目前使用最多的数据库架构之一,尤其是负载比较大的网站,因此对于主从同步的管理也就显得非常重 ...

  7. android AsyncTask实例

    .java package com.example.activitydemoay; import android.app.Activity; import android.content.Intent ...

  8. ZIP打包解包

    linux zip命令的基本用法是: zip [参数] [打包后的文件名] [打包的目录路径] linux zip命令参数列表: -a 将文件转成ASCII模式-F 尝试修复损坏的压缩文件-h 显示帮 ...

  9. shell脚本学习指南

    一.UNIX各个工具所支持的正则表达式: 二.使用sed在某一目录下建立一份目录备份: find /home/foo/ -type d -print | #找出/home/foo/目录下所有目录文件 ...

  10. AI(Adobe Illustrator)简单入门——小熊

    成果: AI里ctrl+z撤销,恢复是ctrl+shift+z. 主要是使用Blob笔刷和橡皮擦工具来做. 一.选择Blog笔刷,选择小熊的颜色. 二.画小熊的头和身子和前脚掌 按住左中括号和右中括号 ...