惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
Microsoft Azure Blog
Microsoft Azure Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog
Y
Y Combinator Blog
博客园_首页
有赞技术团队
有赞技术团队
博客园 - Franky
腾讯CDC
G
Google Developers Blog
Recent Announcements
Recent Announcements
博客园 - 【当耐特】
D
Docker
The GitHub Blog
The GitHub Blog
MyScale Blog
MyScale Blog
H
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
A
About on SuperTechFans
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
V
V2EX
U
Unit 42
aimingoo的专栏
aimingoo的专栏
WordPress大学
WordPress大学

博客园 - 挑战

从存储过程中读取相关信息 Blend Step by Step书籍笔记(第一章) WPF非轮询方式实时更新数据库变化SqlDependency 动态解析XAML文本构建WPF的UI 解决为'*********' 的游标已存在问题 数据表死锁查询和处理 SQL Server操作XML(六)XML FLOWR SQL Server操作XML(五)XML Query-XQuery SQL Server操作XML(三)OPENXML函数功能 SQL Server操作XML(二)XML子句实例 SQL Server操作XML(一)XML子句 数据绑定 最为详尽的WPF类继承关系 LinQ数据访问 WPF Diagram Designer Part 3:连接Item 照猫画虎WPF之二数据绑定 照猫画虎WPF之一:命名空间 解决WPF部署后客户端访问安全性问题 C#读取文本播放相应语音
SQL Server操作XML(四)XML数据类型
挑战 · 2012-06-12 · via 博客园 - 挑战

(1)定义

--表中定义 

CREATE TABLE [dbo].[Invoice](
    [Invoice_ID] [int] NULL,
    [SalesDate] [date] NULL,
    [ItemList] [xml] NULL    
)
--程序中定义

declare @xdoc xml;

--隐式类型转换

SET @xdoc =’

<Customer CustomerID="VINET" ContactName="Paul Henriot">   
    <Order CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00">      
      <OrderDetail OrderID="10248" ProductID="11" Quantity="12"/>     
    </Order>
 </Customer>‘

--显式类型转换

SET @xdoc=CAST('') AS XML 或CONVERT(XML,'')

 --Untype XML

CREATE TABLE Orders
(OrderID int IDENTITY(1,1),
 CustomerID int,
 OrderDetails xml)
--可以插入任意字符串到Untype XML字段中
INSERT Orders VALUES('1','<Product ID="1" QTY="100"/><Product ID="2" QTY="100"/>')

--Type XML 

--声明XML 的Schema 结构,用于Type验证

--通常利用 FOR XML schema自动生成

CREATE XML SCHEMA COLLECTION SalesSchema
AS
'<?xml version="1.0" standalone="yes"?>
<xs:schema id="Sales" xmlns="http://www.gocean.com.cn" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Sales" msdata:IsDataSet="true" msdata:Locale="zh-CN">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Product">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ID" type="xs:int" minOccurs="0" />
              <xs:element name="Name" type="xs:string" minOccurs="0" />
              <xs:element name="Qty" type="xs:int" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

 --声明Type类型的XML 字段

CREATE TABLE Orders
(OrderID int IDENTITY(1,1),
 CustomerID int,
 OrderDetail xml (SalesSchema))

 --符合Schema中Type规范

insert orders values(1,'<Sales><Product><ID>1</ID><Name>p1</Name><Qty>100</Qty></Product></Sales>')

--不符合Schema中Type规范

insert orders values(1,'<Salesman><Product><ID>1</ID><Name>p1</Name><Qty>100</Qty></Product></Salesman>')

显式错误:XML 验证: 找不到元素 'Salesman' 的声明。位置: /*:Salesman[1]

 --声明Type类型的XML 字段,可以存入片段,即只需要Type进行验证,对于是否是标准文档结构不验证

CREATE TABLE Orders
(OrderID int IDENTITY(1,1),
 CustomerID int,
 OrderDetail xml (Content SalesSchema))

 --符合Content条件

insert orders values(1,'<Sales><Product><ID>1</ID><Name>p1</Name><Qty>100</Qty></Product></Sales>
<Sales><Product><ID>2</ID><Name>p2</Name><Qty>200</Qty></Product></Sales>')

--声明Type类型的XML 字段,必须存入片段,即不仅需要Type进行验证,而且校验是否是标准文档结构

CREATE TABLE Orders
(OrderID int IDENTITY(1,1),
 CustomerID int,
 OrderDetail xml (DOCUMENT SalesSchema))

--不符合Document结构

insert orders values(1,'<Sales><Product><ID>1</ID><Name>p1</Name><Qty>100</Qty></Product></Sales>
<Sales><Product><ID>2</ID><Name>p2</Name><Qty>200</Qty></Product></Sales>')

 显式错误:XML 验证: XML 实例必须为文档。

--符合Document结构

insert orders values(1,'<Sales><Product><ID>1</ID><Name>p1</Name><Qty>100</Qty></Product>
<Product><ID>2</ID><Name>p2</Name><Qty>200</Qty></Product></Sales>')