SQL XML 字段操作

---------------------------------------------------一
DECLARE @myDoc xml      
SET @myDoc =
'<root>      
    <item ID="1">      
    </item>      
</root>'      
--SELECT @myDoc      
-- 插入item的第1个子节点,此时不需要指定as first或as last
SET @myDoc.modify(N'      
insert <author>张洪举</author>
into (/root/item)[1]')
SELECT @myDoc      
-- 插入item的第2个子节点,as first指定插入到同级子节点的前面      
SET @myDoc.modify(N'      
insert <title>SQL Server 2005开发宝典</title>
as first into (/root/item)[1]')      
SELECT @myDoc      
-- 插入第2个item节点
SET @myDoc.modify('      
insert <item ID="2"></item>
into (/root)[1]')      
SELECT @myDoc  
-- 向第2个item中插入第1个子节点  
SET @myDoc.modify('      
insert <title>SQL Server 2005的新增功能</title>
as first into (/root/item)[2]')      
SELECT @myDoc
GO
---------------------------------------------------二
DECLARE @myDoc xml
SET @myDoc = '<root>      
    <item ID="1">      
    </item>      
</root>'  
SELECT @myDoc
SET @myDoc.modify('      
insert (
      <title>SQL Server 2005开发宝典</title>,
      <author>张洪举</author>
       )
into (/root/item)[1]');
SELECT @myDoc  
GO
---------------------------------------------------三
DECLARE @myDoc xml      
SET @myDoc = N'<root>      
    <item ID="1">
        <title>Ajax实战</title>
        <author>张洪举</author>
    </item>
    <item ID="2">
        <title>Ajax实战</title>
        <author>张洪举</author>
    </item>        
</root>'  
SELECT @myDoc
SET @myDoc.modify(N'      
insert attribute ShipTo {"广州"}
into (/root/item[@ID=1])[1]');

SET @myDoc.modify(N'      
insert attribute ShipVia {"UPS"}
into (/root/item[@ID=1])[1]');

SELECT @myDoc      
-- 通过一个sql变量指定要插入属性ShipDate的值          
DECLARE @ShipDate char(11)          
SET @ShipDate='2006-01-23Z'          
SET @myDoc.modify('          
insert attribute ShipDate {sql:variable("@ShipDate") cast as xs:date ?}          
into (/root/item[@ID=1])[1]') ;         
SELECT @myDoc          
-- 插入多个属性,属性之间使用逗号分隔,并包含在括号内          
SET @myDoc.modify('          
insert (           
        attribute PostCode {"253020" },          
        attribute Weight {"1.5"}          
        )          
into (/root/item[@ID=1])[1]');
SELECT @myDoc
GO
---------------------------------------------------四
DECLARE @myDoc xml      
SET @myDoc = N'<root>      
    <item ID="1">
        <title>Ajax实战</title>
        <author>张洪举</author>
    </item>
    <item ID="2">
        <title>ASP.NET实战</title>
        <author>卢桂章</author>
    </item>
</root>'  
SET @myDoc.modify('          
insert <!-- 注释 -->          
after (/root/item[@ID=2]/title)[1]');
SELECT @myDoc
GO
---------------------------------------------------五
DECLARE @myDoc xml      
SET @myDoc = N'<root>      
    <item ID="1">
        <title>Ajax实战</title>
        <author>张洪举</author>
    </item>
    <item ID="2">
        <title>ASP.NET实战</title>
        <author>卢桂章</author>
    </item>
</root>'  
SET @myDoc.modify(N'          
insert <desc><![CDATA[ <送货方式>上门<价款>未收]]></desc>
into (/root/item[@ID=2])[1] ') ;
SELECT @myDoc
GO
---------------------------------------------------六
DECLARE @myDoc xml      
SET @myDoc = N'<root>      
    <item ID="1">
        <title>Ajax实战</title>
        <author>张洪举</author>
    </item>
</root>'  
SET @myDoc.modify(N'
insert text{"订单列表"}
as first into (/root)[1]');
SELECT @myDoc
GO
---------------------------------------------------六
use jobproject
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
---------------------------------------------------七
DECLARE @myDoc xml
SET @myDoc = '<?Instructions for=TheWC.exe ?>
<root>
    <!-- 这里是注释 -->
    <item ID="1" ShipTo="广州">这里是文本
        <title>Ajax实战</title>
        <author>张洪举</author>
    </item>
    <item ID="2">
        <title>ASP.NET实战</title>
        <author>卢桂章</author>
    </item>
</root>'
SELECT @myDoc
-- 删除注释
SET @myDoc.modify('
delete /root/comment()
')
SELECT @myDoc
-- 删除所有指令
SET @myDoc.modify('
delete //processing-instruction()
')
SELECT @myDoc
-- 删除ID为1的item中的文本节点
SET @myDoc.modify('
delete /root/item[@ID=1]/text()
')
SELECT @myDoc
-- 删除一个属性
SET @myDoc.modify('
delete /root/item[@ID=1]/@ShipTo
')
SELECT @myDoc
-- 删除一个元素
SET @myDoc.modify('
delete /root/item[@ID=2]/author
')
SELECT @myDoc

-- 删除ID为2的item节点
SET @myDoc.modify('
delete /root/item[@ID=2]
')
SELECT @myDoc
GO
---------------------------------------------------八
UPDATE MyCustomer
SET CustomerItem.modify('
declare namespace CS="http://schemas.mybook.com/customerschemas";
delete /CS:customer/item[@ID=2]
');
SELECT CustomerItem FROM MyCustomer;
GO
---------------------------------------------------九
DECLARE @myDoc xml
SET @myDoc = '<root>
    <item ID="1">
        <title>Ajax实战</title>
        <author>张洪举</author>
    </item>
    <item ID="2">
        <title>ASP.NET实战</title>
        <author>卢桂章</author>
    </item>
</root>'
SELECT @myDoc
-- 更新ID为1的item中的title元素的文本
SET @myDoc.modify('
replace value of (/root/item[@ID=1]/title/text())[1]
with "Ajax实战攻略"
')
SELECT @myDoc
-- 更新属性值
SET @myDoc.modify('
replace value of (/root/item[@ID=2]/@ID)[1]
with "3"
')
SELECT @myDoc
---------------------------------------------------九
declare @xdoc xml
set @xdoc = N'<conn>
                <mysql servername="192.168.1.120,3306">
                  <user name="root" password="" />
                  <user name="dba"   password="" />
                  <user name="user" password="" />
                </mysql>
                <mssql servername="192.168.1.110,1433">
                  <user name="sa"    password="" />
                  <user name="dba"   password="" />
                  <user name="user" password="" />
                </mssql>
                <excel id="1" path="c:/database/" filename="source1.xls" />
                <excel id="2" path="d:/database/" filename="source2.xls" />
                <excel id="3" path="e:/database/" filename="source3.xls" />
             </conn>
            '
-------第一方法
select excel_path = t.c.value(
'@path'     ,'varchar(255)')
      ,excel_name = t.c.value(
'@filename' ,'varchar(255)')
from @xdoc.nodes('/conn/excel') t (c)
where t.c.value(
'@id', 'int') = 1
-------第二方法
select excel_path = @xdoc.value('(/conn/excel[@id="1"]/@path)[1]', 'varchar(255)')
       ,excel_name = @xdoc.value('(/conn/excel[@id="1"]/@filename)[1]', 'varchar(255)')
-------第三方法
declare @excel_id int
    set @excel_id = 1
select excel_path = @xdoc.value('(/conn/excel[@id=sql:variable("@excel_id")]/@path)[1]', 'varchar(255)')
      ,excel_name = @xdoc.value('(/conn/excel[@id=sql:variable("@excel_id")]/@filename)[1]', 'varchar(255)')

 

原文地址:http://hi.baidu.com/kjkj911/blog/item/d6d0f81f86a0e302304e15c4.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值