XML
declare @xmlDoc xml
set @xmlDoc='
<people>
<star id="001">
<name>周杰伦</name>
<job >歌手</job >
<age>50</age>
</star>
<star id="002">
<name>周星驰</name>
<job >导演</job >
<age>45</age>
</star>
<star id="003">
<name>成龙</name>
<job >演员</job >
<age>44</age>
</star>
<star id="004">
<name mark="明天开演唱会">刘德华</name>
<name newmark="全球性的">刘德华</name>
<job >歌手</job >
<age>49</age>
</star>
</people>
'
--修改属性
--set @xmlDoc.modify('replace value of (people/star[@id="004"]/name/@mark)[2] with "修改测试"')
/*
<people>
<star id="001">
<name>周杰伦</name>
<job>歌手</job>
<age>50</age>
</star>
<star id="002">
<name>周星驰</name>
<job>导演</job>
<age>45</age>
</star>
<star id="003">
<name>成龙</name>
<job>演员</job>
<age>44</age>
</star>
<star id="004">
<name mark="明天开演唱会">刘德华</name>
<name newmark="全球性的">刘德华</name>
<job>歌手</job>
<age>49</age>
</star>
</people>
*/
--删除属性
--set @xmlDoc.modify('delete people/star[@id="004"]/name[2]/@newmark')
/*
<people>
<star id="001">
<name>周杰伦</name>
<job>歌手</job>
<age>50</age>
</star>
<star id="002">
<name>周星驰</name>
<job>导演</job>
<age>45</age>
</star>
<star id="003">
<name>成龙</name>
<job>演员</job>
<age>44</age>
</star>
<star id="004">
<name mark="明天开演唱会">刘德华</name>
<name>刘德华</name>
<job>歌手</job>
<age>49</age>
</star>
</people>
*/
--插入属性
--set @xmlDoc.modify('insert attribute 插入{"这里是插入"} into (/people/star[@id=0001]/name)[1]')
/*
<people>
<star id="001">
<name 插入="这里是插入">周杰伦</name>
<job>歌手</job>
<age>50</age>
</star>
<star id="002">
<name>周星驰</name>
<job>导演</job>
<age>45</age>
</star>
<star id="003">
<name>成龙</name>
<job>演员</job>
<age>44</age>
</star>
<star id="004">
<name mark="明天开演唱会">刘德华</name>
<name newmark="全球性的">刘德华</name>
<job>歌手</job>
<age>49</age>
</star>
</people>
*/
--查看属性
--select @xmlDoc.value('(/people/star/name/@mark)[1]', 'nvarchar(max)')
/*
明天开演唱会
*/
--删除节点
--set @xmlDoc.modify('delete /people/star[@id=0004]')
/*
<people>
<star id="001">
<name>周杰伦</name>
<job>歌手</job>
<age>50</age>
</star>
<star id="002">
<name>周星驰</name>
<job>导演</job>
<age>45</age>
</star>
<star id="003">
<name>成龙</name>
<job>演员</job>
<age>44</age>
</star>
</people>
*/
--修改节点
--set @xmlDoc.modify('replace value of (/people/star[1]/job/text())[1] with "耕田"')
/*
<people>
<star id="001">
<name>周杰伦</name>
<job>耕田</job>
<age>50</age>
</star>
<star id="002">
<name>周星驰</name>
<job>导演</job>
<age>45</age>
</star>
<star id="003">
<name>成龙</name>
<job>演员</job>
<age>44</age>
</star>
<star id="004">
<name mark="明天开演唱会">刘德华</name>
<name newmark="全球性的">刘德华</name>
<job>歌手</job>
<age>49</age>
</star>
</people>
*/
--set @xmlDoc.modify('replace value of (/people/star[@id=004]/job/text())[1] with "杀手"')
/*
<people>
<star id="001">
<name>周杰伦</name>
<job>歌手</job>
<age>50</age>
</star>
<star id="002">
<name>周星驰</name>
<job>导演</job>
<age>45</age>
</star>
<star id="003">
<name>成龙</name>
<job>演员</job>
<age>44</age>
</star>
<star id="004">
<name mark="明天开演唱会">刘德华</name>
<name newmark="全球性的">刘德华</name>
<job>杀手</job>
<age>49</age>
</star>
</people>
*/
--set @xmlDoc.modify('replace value of (/people/star/@id)[1] with 3.14159265')
/*
<people>
<star id="3.14159265">
<name>周杰伦</name>
<job>歌手</job>
<age>50</age>
</star>
<star id="002">
<name>周星驰</name>
<job>导演</job>
<age>45</age>
</star>
<star id="003">
<name>成龙</name>
<job>演员</job>
<age>44</age>
</star>
<star id="004">
<name mark="明天开演唱会">刘德华</name>
<name newmark="全球性的">刘德华</name>
<job>歌手</job>
<age>49</age>
</star>
</people>
*/
--插入节点
--set @xmlDoc.modify('insert <star id="4564564"> <name>麦克</name><job>演员</job><age>44</age></star> before (/people/star)[4]')
/*
<people>
<star id="001">
<name>周杰伦</name>
<job>歌手</job>
<age>50</age>
</star>
<star id="002">
<name>周星驰</name>
<job>导演</job>
<age>45</age>
</star>
<star id="003">
<name>成龙</name>
<job>演员</job>
<age>44</age>
</star>
<star id="4564564">
<name>麦克</name>
<job>演员</job>
<age>44</age>
</star>
<star id="004">
<name mark="明天开演唱会">刘德华</name>
<name newmark="全球性的">刘德华</name>
<job>歌手</job>
<age>49</age>
</star>
</people>
*/
--查找
--找到周杰伦(第一个name)
--select @xmlDoc.value('(/people/star/name)[1]', 'nvarchar(200)')
/*
周杰伦
*/
--找到周星驰的所有(一行显示,周星驰在位置第二)
--select @xmlDoc.value('(/people/star)[2]', 'nvarchar(60)')
/*
周星驰导演45
*/
--列出全部
--select @xmlDoc.value('(/people)[1]', 'nvarchar(60)')
/*
周杰伦歌手50周星驰导演45成龙演员44刘德华刘德华歌手49
*/
--找到第二个name
--select @xmlDoc.value('(/people/star/name)[2]', 'nvarchar(60)')
/*
周星驰
*/
--查看第二个ID
--select @xmlDoc.value('(/people/star/@id)[2]', 'nvarchar(max)')
/*
002
*/
--使用count 函数
SELECT CAST( CAST (@xmlDoc.query('count(/people/star) ') AS VARCHAR(20)) AS int)AS count