WEB开发网
开发学院数据库MSSQL Server SQL Server 2005的XML数据修改语言 阅读

SQL Server 2005的XML数据修改语言

 2007-05-19 09:42:54 来源:WEB开发网   
核心提示: -- 创建XML架构集合CREATE XML SCHEMA COLLECTION MySchemasASN'<?xml version = "1.0"?><xsd:schema targetNamespace="http://sche

-- 创建XML架构集合

CREATE XML SCHEMA COLLECTION MySchemas
AS
N'<?xml version = "1.0"?>
<xsd:schema targetNamespace="http://schemas.mybook.com/customerschemas"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="customer">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element maxOccurs="unbounded" name="item">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="customername" type="xsd:string"/>
              <xsd:element name="address" type="xsd:string"/>
              <xsd:element name="phone" type="xsd:string"/>
              <xsd:element name="contact" type="xsd:string"/>
            </xsd:sequence>
            <xsd:attribute name="ID" type="xsd:int"/>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>';
GO
-- 创建包含xml数据类型列的表
CREATE TABLE MyCustomer
  (CustomerID int IDENTITY PRIMARY KEY,
   CustomerItem xml(MySchemas));
GO
-- 向表中插入XML,该XML应当符合http://schemas.mybook.com/customerschemas命名空间架构的定义
INSERT INTO MyCustomer
VALUES
(N'<C:customer xmlns:C="http://schemas.mybook.com/customerschemas">
  <item ID="1">
    <customername>北方书城</customername>
    <address>北京市海淀区知春路22号</address>
    <phone>2222222</phone>
    <contact>刘先生</contact>
  </item>
</C:customer>');
-- 使用XML DML insert插入另一个item节点到XML中
UPDATE MyCustomer
SET CustomerItem.modify('
declare namespace CS="http://schemas.mybook.com/customerschemas";
insert (<item ID="2">
    <customername>东图大厦</customername>
    <address>长春市朝阳大街99号</address>
    <phone>1111111</phone>
    <contact>孙小姐</contact>
  </item>)
into (/CS:customer)[1] ')
WHERE CustomerID=1;
SELECT CustomerItem
FROM Mycustomer;
GO

上一页  1 2 3 4 5 6 7 8  下一页

Tags:SQL Server XML

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接