SQL读取XML字段类型的信息

USE CSOS_NEW_2
GO

--(1)定义临时表
DECLARE @table TABLE(id INT IDENTITY(1,1),XMLDetail XML)
DECLARE @xml XML
SET @xml='<EBPCaseDetailType>
  <openReason xmlns="http://www.ebay.com/marketplace/resolution/v1/services">Item not received</openReason>
  <decisionReason xmlns="http://www.ebay.com/marketplace/resolution/v1/services">11002</decisionReason>
  <decisionDate xmlns="http://www.ebay.com/marketplace/resolution/v1/services">2013-06-25T18:09:19Z</decisionDate>
  <decision xmlns="http://www.ebay.com/marketplace/resolution/v1/services">SELLER_FAULT</decision>
  <FVFCredited xmlns="http://www.ebay.com/marketplace/resolution/v1/services">false</FVFCredited>
  <notCountedInBuyerProtectionCases xmlns="http://www.ebay.com/marketplace/resolution/v1/services">false</notCountedInBuyerProtectionCases>
  <globalId xmlns="http://www.ebay.com/marketplace/resolution/v1/services">EBAY_UK</globalId>
  <responseHistory xmlns="http://www.ebay.com/marketplace/resolution/v1/services">
    <author>
      <role>EBAY</role>
    </author>
    <activity>agentResolve</activity>
    <creationDate>2013-06-25T18:10:03Z</creationDate>
  </responseHistory>
  <responseHistory xmlns="http://www.ebay.com/marketplace/resolution/v1/services">
    <author>
      <role>BUYER</role>
    </author>
    <activity>contactCustomerSupport</activity>
    <creationDate>2013-06-25T12:24:53Z</creationDate>
  </responseHistory>
  <responseHistory xmlns="http://www.ebay.com/marketplace/resolution/v1/services">
    <author>
      <role>EBAY</role>
    </author>
    <activity>systemExpireGrace</activity>
    <creationDate>2013-06-24T16:01:13Z</creationDate>
  </responseHistory>
  <responseHistory xmlns="http://www.ebay.com/marketplace/resolution/v1/services">
    <note>Nothing has yet been received, if the item can be sent this week then please send it. If not then a refund please. Thankyou</note>
    <author>
      <role>BUYER</role>
    </author>
    <activity>create</activity>
    <creationDate>2013-06-17T05:34:49Z</creationDate>
  </responseHistory>
  <agreedRefundAmount xmlns="http://www.ebay.com/marketplace/resolution/v1/services">0</agreedRefundAmount>
  <paymentDetail xmlns="http://www.ebay.com/marketplace/resolution/v1/services">
    <moneyMovement id="M.1">
      <type>REFUND</type>
      <fromParty>
        <role>SELLER</role>
      </fromParty>
      <toParty>
        <role>BUYER</role>
      </toParty>
      <amount currencyId="GBP">4.19</amount>
      <paymentMethod>PAYPAL</paymentMethod>
      <paypalTransactionId>5NE10254S0169263L</paypalTransactionId>
      <status>SUCCESS</status>
      <transactionDate>2013-06-25T18:09:18Z</transactionDate>
    </moneyMovement>
  </paymentDetail>
  <detailStatus xmlns="http://www.ebay.com/marketplace/resolution/v1/services">4</detailStatus>
  <initialBuyerExpectation xmlns="http://www.ebay.com/marketplace/resolution/v1/services">103</initialBuyerExpectation>
</EBPCaseDetailType>';

--(2)创建测数据
INSERT @table (XMLDetail )
VALUES  (
          @xml  -- XMLDetail - xml
          )

SELECT *
FROM @table

--(3)读取XML字段的数据
;
WITH XMLNAMESPACES('http://www.ebay.com/marketplace/resolution/v1/services' AS xs)
SELECT  id,XMLDetail.value('(EBPCaseDetailType/xs:paymentDetail/xs:moneyMovement/xs:type)[1]','nvarchar(max)') AS 'Paymentype',
XMLDetail.value('(EBPCaseDetailType/xs:paymentDetail/xs:moneyMovement/xs:fromParty/xs:role)[1]','nvarchar(max)') AS 'Refundrole',
XMLDetail.value('(EBPCaseDetailType/xs:paymentDetail/xs:moneyMovement/xs:paypalTransactionId)[1]','nvarchar(max)') AS 'paypalTransactionId',
XMLDetail.value('(EBPCaseDetailType/xs:openReason)[1]','nvarchar(max)') AS 'openReason'
FROM @table

显示结果如下:

id  Paymentype  Refundrole    paypalTransactionId    openReason
1   REFUND       SELLER         5NE10254S0169263L  Item not received 

  

时间: 2024-07-30 18:49:58

SQL读取XML字段类型的信息的相关文章

sql2005的xml字段类型在.net中的应用

sql|sql2005|xml    今天要介绍的就是sql2005的xml字段类型在.net中的应用.调用过程是:先运用并行化的办法把xml字段类型中的数据转换成Model对象,对Model对象操作后,再运用串行化的方法把Model对象转变成xml格式,最后存储到数据库中.       我认为如果把复杂的业务关系数据存储在xml字段中,可简化数据库的设计,方便业务的处理.        这里写了个小demo:            假如我们有很多店铺信息,每个店铺都有一个ShopID, 所以我

SQL对Xml字段的操作

原文:SQL对Xml字段的操作   T-Sql操作Xml数据 一.前言 SQL Server 2005 引入了一种称为 XML 的本机数据类型.用户可以创建这样的表,它在关系列之外还有一个或多个 XML 类型的列:此外,还允许带有变量和参数.为了更好地支持 XML 模型特征(例如文档顺序和递归结构),XML 值以内部格式存储为大型二进制对象 (BLOB). 用户将一个XML数据存入数据库的时候,可以使用这个XML的字符串,SQL Server会自动的将这个字符串转化为XML类型,并存储到数据库中

sql alter 修改字段类型与名称的方法

sql alter 修改字段类型与名称的方法 ALTER TABLE "table_name" [改变方式] 加一个栏位: ADD "栏位 1" "栏位 1 资料种类" mysql教程> CREATE TABLE Employee(     ->     id            int,     ->     first_name    VARCHAR(15),     ->     last_name     VAR

SQL SERVER的字段类型说明_数据库其它

SQL SERVER提供的说明.  bit:0或1的整型数字  int:从-2^31(-2,147,483,648)到2^31(2,147,483,647)的整型数字  smallint:从-2^15(-32,768)到2^15(32,767)的整型数字  tinyint:从0到255的整型数字  decimal:从-10^38到10^38-1的定精度与有效位数的数字  numeric:decimal的同义词  money:从-2^63(-922,337,203,685,477.5808)到2^

sql server 中字段类型及说明

server 数据类型 说 明 同 义 Bit 1位,值为0或1 Int Integer 4字节,值为-2^31~2^31-1 Smallint 2字节,值为-2^15~2^15-1 Tinyint 1字节,值为0~255 Decimal (p,s) 数字数据,固定精度为P,宽度为S Numeric Money 8字节,存放货币类型,值为-2^63~2^63-1 Small money 4字节,存放货币类型,值为-214748.3648~+214748.3647近似数值数据类型 Float (n

SQL SERVER中字段类型及说明

server 数据类型     说 明     同 义 Bit     1位,值为0或1     Int     Integer     4字节,值为-2^31~2^31-1     Smallint     2字节,值为-2^15~2^15-1     Tinyint     1字节,值为0~255     Decimal (p,s)     数字数据,固定精度为P,宽度为S     Numeric     Money     8字节,存放货币类型,值为-2^63~2^63-1     Sma

SQL中varchar和nvarchar字段类型的区别

sql|区别|varchar|字段类型 在SQL Server 2000中,NVARCHAR和VARCHAR区别是什么? SQL Server提供两种数据类型来存储字符信息.在如何在SQL Server或应用程序中使用方面,这两种数据类型大致是一样的.差别在于nvarchar是用于存储处理数据库图表中多语言数据的Unicode数据.其它语言有一套需要保存的字符代码的扩展集,这种数据类型支持这样的扩展.因为需要存储其它语言的扩展字符代码,nvarchar占用的空间是varchar的两倍.     

sqlserver字段类型详解(转)

bit    整型 bit数据类型是整型,其值只能是0.1或空值.这种数据类型用于存储只有两种可能值的数据,如Yes 或No.True 或False .On 或Off.    注意:很省空间的一种数据类型,如果能够满足需求应该尽量多用.   tinyint   整型 tinyint 数据类型能存储从0到255 之间的整数.它在你只打算存储有限数目的数值时很有用.这种数据类型在数据库中占用1 个字节.  注意:如果bit类型太单调不能满足您的需求,您可以考虑用tinyint类型,因为这个类型相对也

Javascript 读取操作Sql中的Xml字段

 首先我们在页面中放一个TextBox 用来放Xml字段.切记:不可用Label,因为万一Xml字段信息中有""这种符号时,页面会产生Js错误. 接下去,就是重点了.付JS代码: ? 1 2 3 4 5 6 7 8 9 function createXml(str){  if(document.all){  var xmlDom=new ActiveXObject("Microsoft.XMLDOM")  xmlDom.loadXML(str)  return xm