========插入单节点========

-将<author>Erik E. Ray</author>插入@category="WEB"的第一个book节点的author节点前
set @data.modify('insert <author>Erik E. Ray</author> before (/bookstore/book[@category="WEB"]/author)[1]')
/*output:
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>Erik E. Ray</author>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
*/
--将<author>Erik E. Ray</author>插入@category="WEB"的第一个book节点的author节点前
DECLARE @authorname VARCHAR(15)
SELECT @authorname = ' Erik E. Ray'
SET @data.modify('insert element author {sql:variable("@authorname")} before (/bookstore/book[@category="WEB"]/author)[1]')
/*output:
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>Erik E. Ray</author>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
*/
--将<author>Erik E. Ray</author>插入@category="WEB"的第一个book节点的第一个author节点后面
set @data.modify('insert <author>Erik E. Ray</author> after (/bookstore/book[@category="WEB"]/author)[1]')
/*output:
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
<author>Erik E. Ray</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
*/
--将<author>Erik E. Ray</author>插入@category="WEB"的第一个book节点的最前面
set @data.modify('insert <author>Erik E. Ray</author> as first into (/bookstore/book[@category="WEB"])[1]')
/*output:
<book category="WEB">
<author>Erik E. Ray</author>
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
*/
--将<author>Erik E. Ray</author>插入@category="WEB"的第一个book节点的最后面
set @data.modify('insert <author>Erik E. Ray</author> as last into (/bookstore/book[@category="WEB"])[1]')
/*output:
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
  <price>49.99</price>
  <author>Erik E. Ray</author>
</book>
*/

复制代码

========插入多节点========

--将<author>Erik E. Ray1</author>,<author>Erik E. Ray2</author>插入@category="WEB"的第一个book节点的最后
set @data.modify('insert (<author>Erik E. Ray1</author>,<author>Erik E. Ray2</author> )as last into(/bookstore/book[@category="WEB"])[1]')
/*output:
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
<author>Erik E. Ray1</author>
<author>Erik E. Ray2</author>
</book>*/

复制代码

========插入文本节点======

--将文本节点“at first”&gt;插入第一个book节点的最前面
set @data.modify('insert text{"at first"} as first into (/bookstore[1]/book[1])')
/*output:
<book category="COOKING" var="变量插入">
at first
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book> */

复制代码

========插入注释========

--将注释“<!—插入注释-->”&gt;插入第一个book节点的year节点的前面
set @data.modify('insert <!--插入评论--> before (/bookstore/book[1]/year)[1]')
/*output:
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<!--插入注释-->
<year>2005</year>
<price>30.00</price>
</book> */

复制代码

========根据条件插入========

--如果第一个book节点存在第二个author节点则为第一个book节点添加<text>this is a 1 step</text> 否则添加<text>this is a 2 step</text>
set @data.modify('insert if (/bookstore/book[1]/author[2]) then (<text>this is a 1 step</text>)else ( <text>this is a 2 step</text> ) as first into (/bookstore/book)[1]')
/*output:
<book category="COOKING">
<text>this is a 2 step</text>
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
*/

复制代码

========利用变量插入==========

--利用变量插入新的book节点
declare @category varchar(50)
declare @text varchar(50)
set @category='IT';
set @text='this is a IT book'
set @data.modify('insert <book category="{sql:variable("@category")}">{sql:variable("@text")}</book> as first into (/bookstore)[1]')
/*output:
<bookstore>
<book category="IT">this is a IT book</book>
<book category="COOKING">
……..
</bookstore>
*/

复制代码