2006-10-16

SQL Server 2005的XML数据修改语言

来源: 本站收集整理 作者:佚名 评论 0 条
 

a

图1 向XML中插入节点

(2)插入多个元素到文档中

在下面的示例中,将<title>和<author>元素插入到了item节点中。元素之间使用逗号分隔,并包含在括号中。

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

(3)插入属性到文档中

在下面的示例中,向XML文档中插入了多个属性。每次插入属性后,SELECT语句都会显示结果,最终执行结果如图2所示。

DECLARE @myDoc xml

SET @myDoc = '<root>

<item ID="1">

<title>Ajax实战</title>

<author>张洪举</author>

</item>

</root>'

SELECT @myDoc

SET @myDoc.modify('

insert attribute ShipTo {"广州"}

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

a

图2插入属性到XML中

(4)插入注释节点

在下面的示例中,将注释节点插入到ID为2的item节点中<title>元素的后面。

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>'

SET @myDoc.modify('

insert <!-- 注释 -->

after (/root/item[@ID=2]/title)[1]');

SELECT @myDoc

GO

插入注释后XML的内容如下:

<root>

<item ID="1">

<title>Ajax实战</title>

<author>张洪举</author>

</item>

<item ID="2">

<title>ASP.NET实战</title>

<!-- 注释 -->

<author>卢桂章</author>

</item>

</root>

(5)使用CDATA部分插入数据

当插入的文本中包含有XML无效字符(如“<”或“>”)时,可以使用CDATA部分插入数据。参考下面的示例:

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>'

SET @myDoc.modify('

insert <desc><![CDATA[ <送货方式>上门<价款>未收]]></desc>

into (/root/item[@ID=2])[1] ') ;

SELECT @myDoc

GO




共5页: 上一页 [1] 2 [3] [4] [5] 下一页
(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:SQL Server连接失败错误分析与排除  
下一篇:用SQL 2005的ROW_NUMBER() 实现分页功能
    评论加载中…
 推荐文章
     

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号