今天天气不错,园子大丰收~~:

SQL对表中XML列的查询

先看下数据表:

详细XML:

<Roles>
<Role>CA</Role>
<Role>CentralSite</Role>
<Role>DC</Role>
</Roles>

查询1:

Select Roles.value('(/Roles/Role)[1]', 'nvarchar(20)')
From dbo.MachineConfigs

结果:

 

查询2:

select (select Roles from MachineConfigs where
ID='F16C7025-4E67-4F7F-814D-04D6B5B9387C' ).query('(/Roles/Role[1])')

结果:

<Role>CA</Role>

 

查询3:

 

SELECT nref.query('.') LastName FROM MachineConfigs CROSS
APPLY Roles.nodes(
'/Roles/Role') AS R(nref)

SELECT nref.query('.') LastName FROM MachineConfigs CROSS
APPLY Roles.nodes(
'/Roles/Role[1]') AS R(nref)

结果:

前者:            后者:(有区别的哦

 

判断存在:

SELECT Roles.exist('/Roles/Role') FROM MachineConfigs

结果:

1

1

1

1

相关文章:http://book.51cto.com/art/201007/215493.htm

posted @ 2010-08-10 17:51  Jimson.Ma  阅读(323)  评论(2编辑  收藏  举报

Email: 20045912@163.com MSN: 20045912@163.com