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. 【Android】Mac安装EasyTether导致无法识别设备的问题

    正文 想让手机走PC网络,然后抓包,于是搜索一番后安装了一个叫EasyTether的软件.还没来得及测试,就忙着写代码去了,重启MAC以后就发现连不上手机了,一开始并没有怀疑是 EasyTether的 ...

  2. 自己用js写的日历(在考勤中使用,显示员工的日期的考勤情况)

    1.HTML部分 <div id="AttendanceDataDetailDiv"> <div class="A_close"> &l ...

  3. yii2 输出xml格式数据

    作者:白狼 出处:http://www.manks.top/yii2_xml_response.html.html本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文 ...

  4. [oracle]数据库语言分类

    一般来说,数据库语言可以分成以下5大类: 1.数据定义语言DDL(Data Definition Language),用于改变数据库结构,包括创建.修改和删除数据库对象.包括create(创建).al ...

  5. Mongodb Manual阅读笔记:CH6 聚合

    6 聚合 Mongodb Manual阅读笔记:CH2 Mongodb CRUD 操作Mongodb Manual阅读笔记:CH3 数据模型(Data Models)Mongodb Manual阅读笔 ...

  6. mysql datetime查询异常

    mysql datetime查询异常 异常:Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp (2011 ...

  7. top:failed tty get 错误

    运行命令,ps -ef | grep test | grep -v test | awk '{ print $2 }' | xargs top -H -p 想看test的实时状态,结果报了错,查了一下 ...

  8. 在eclipse中使用maven创建springMVC项目

    一.在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 ...

  9. 关于 RTL8723BS 同时开启 STA/AP 模式

    最近接到一个调试 wifi 驱动的任务,使用的是 rtl8723bs 芯片组.要求是让无线设备工作在 station 模式的时候同时开启一个 ap 热点.简单来讲就是连接其他 wifi 的同时发出一个 ...

  10. 将treeview控件内容导出图片

    项目中有一项需求,需要将项目中的treeview控件展示的树状结构直接导成一张图片.网上方法很多,但很多都是屏幕截屏,我的解决思路是新建一个用户控件,将主窗体的Treeview的数据传给用户控件(不要 ...