元素移动
DECLARE @x XML
SELECT @x = '
<Peoples>
<People NAME="周星驰"/>
<People NAME="刘德华"/>
<People NAME="周杰伦"/>
</Peoples>'
------------------------------------------------------------
-- "周杰伦" 向上移动一层
------------------------------------------------------------
set @x.modify('
insert /Peoples/People[@NAME="周杰伦"]
before (/Peoples/People[. << (/Peoples/People[@NAME="周杰伦"])[1]])
[last()]
')
SET @x.modify ('
delete /Peoples/People[@NAME="周杰伦"]
[. is (/Peoples/People[@NAME="周杰伦"])[last()]]
')
SELECT @x
/*
<Peoples>
<People NAME="周星驰" />
<People NAME="周杰伦" />
<People NAME="刘德华" />
</Peoples>
*/
------------------------------------------------------------
-- "周星驰" 向下移动一层
------------------------------------------------------------
set @x.modify('
insert /Peoples/People[@NAME="周星驰"]
before (/Peoples/People[. >> (/Peoples/People[@NAME="周星驰"])[1]])
[last()]
')
SET @x.modify ('
delete (/Peoples/People[@NAME="周星驰"])[1]
')
SELECT @x
/*
<Peoples>
<People NAME="周杰伦" />
<People NAME="周星驰" />
<People NAME="刘德华" />
</Peoples>
*/
SELECT @x = '
<Peoples>
<People NAME="周星驰"/>
<People NAME="刘德华"/>
<People NAME="周杰伦"/>
</Peoples>'
------------------------------------------------------------
-- "周杰伦" 向上移动一层
------------------------------------------------------------
set @x.modify('
insert /Peoples/People[@NAME="周杰伦"]
before (/Peoples/People[. << (/Peoples/People[@NAME="周杰伦"])[1]])
[last()]
')
SET @x.modify ('
delete /Peoples/People[@NAME="周杰伦"]
[. is (/Peoples/People[@NAME="周杰伦"])[last()]]
')
SELECT @x
/*
<Peoples>
<People NAME="周星驰" />
<People NAME="周杰伦" />
<People NAME="刘德华" />
</Peoples>
*/
------------------------------------------------------------
-- "周星驰" 向下移动一层
------------------------------------------------------------
set @x.modify('
insert /Peoples/People[@NAME="周星驰"]
before (/Peoples/People[. >> (/Peoples/People[@NAME="周星驰"])[1]])
[last()]
')
SET @x.modify ('
delete (/Peoples/People[@NAME="周星驰"])[1]
')
SELECT @x
/*
<Peoples>
<People NAME="周杰伦" />
<People NAME="周星驰" />
<People NAME="刘德华" />
</Peoples>
*/